Is it possible to rearrange tab items in tab control in wpf?
found a solution in the MSDN forum.
Here is the link:
DragDrop TabItem
Here is the solution:
C# solution
WPF code:
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="AllowDrop" Value="True"/>
<EventSetter Event="PreviewMouseMove" Handler="TabItem_PreviewMouseMove"/>
<EventSetter Event="Drop" Handler="TabItem_Drop"/>
</Style>
</TabControl.Resources>
<TabItem Header="Tabitem 1"/>
<TabItem Header="Tabitem 2"/>
<TabItem Header="Tabitem 3"/>
<TabItem Header="Tabitem 4"/>
<TabItem Header="Tabitem 5"/>
</TabControl>
C# code behind:
private void TabItem_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (!(e.Source is TabItem tabItem))
{
return;
}
if (Mouse.PrimaryDevice.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(tabItem, tabItem, DragDropEffects.All);
}
}
private void TabItem_Drop(object sender, DragEventArgs e)
{
if (e.Source is TabItem tabItemTarget &&
e.Data.GetData(typeof(TabItem)) is TabItem tabItemSource &&
!tabItemTarget.Equals(tabItemSource) &&
tabItemTarget.Parent is TabControl tabControl)
{
int targetIndex = tabControl.Items.IndexOf(tabItemTarget);
tabControl.Items.Remove(tabItemSource);
tabControl.Items.Insert(targetIndex, tabItemSource);
tabItemSource.IsSelected = true;
}
}