Is it possible to rearrange tab items in tab control in wpf?

冷暖自知 提交于 2019-12-20 10:39:23

问题


Is it possible to rearrange tab items in tab control in run time? For example I have 3 tab items which are about cars and 4 tabs about house. I want to be able to rearrange them using drag and drop. Is it possible or it is something fantastic?

I have Tab Control here is XAML.

<TabControl x:Name="tc" Visibility="Collapsed" GotFocus="Focus" AllowDrop="True" >
            </TabControl>

Tab items will be added in runtime. Thanks for helping me!


回答1:


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)
{
    var tabItem = e.Source as TabItem;

    if (tabItem == null)
        return;

    if (Mouse.PrimaryDevice.LeftButton == MouseButtonState.Pressed)
    {
        DragDrop.DoDragDrop(tabItem, tabItem, DragDropEffects.All);
    }
}


private void TabItem_Drop(object sender, DragEventArgs e)
{
    var tabItemTarget = e.Source as TabItem;

    var tabItemSource = e.Data.GetData(typeof(TabItem)) as TabItem;

    if (!tabItemTarget.Equals(tabItemSource))
    {
        var tabControl = tabItemTarget.Parent as TabControl;
        int sourceIndex = tabControl.Items.IndexOf(tabItemSource);
        int targetIndex = tabControl.Items.IndexOf(tabItemTarget);

        tabControl.Items.Remove(tabItemSource);
        tabControl.Items.Insert(targetIndex, tabItemSource);

        tabControl.Items.Remove(tabItemTarget);
        tabControl.Items.Insert(sourceIndex, tabItemTarget);
    }
}



回答2:


When I tried to implement this solution, the drop event was firing twice (moving the tabs but them immediately moving them back). I had to add an integer to keep track of the last tab target index. My solution is in VB.NET

'additional variable
Dim lastTabTargetIndex As Integer = Nothing

Private Sub tc1_PreviewMouseMove(sender As Object, e As MouseEventArgs) Handles tc1.PreviewMouseMove

    Dim Tabi = TryCast(e.Source, TabItem)

    If Tabi Is Nothing Then
        Exit Sub
    Else
        If Mouse.PrimaryDevice.LeftButton = MouseButtonState.Pressed Then
            DragDrop.DoDragDrop(Tabi, Tabi, DragDropEffects.All)
        End If
    End If
End Sub

Private Sub tc1_Drop(sender As Object, e As DragEventArgs) Handles tc1.Drop

    Dim tabItemTarget = TryCast(e.Source, TabItem)
    Dim tabItemSource = TryCast(e.Data.GetData(GetType(TabItem)), TabItem)

    If Not tabItemTarget.Equals(tabItemSource) Then
        Dim tabControl = TryCast(tabItemTarget.Parent, TabControl)
        Dim sourceIndex As Integer = tabControl.Items.IndexOf(tabItemSource)
        Dim targetIndex As Integer = tabControl.Items.IndexOf(tabItemTarget)

        'had to use this extra statement
        If sourceIndex <> lastTabTargetIndex Then
            'assign lastTabTargetIndex here
            lastTabTargetIndex = targetIndex
            tabControl.Items.Remove(tabItemSource)
            tabControl.Items.Insert(targetIndex, tabItemSource)
            tabControl.Items.Remove(tabItemTarget)
            tabControl.Items.Insert(sourceIndex, tabItemTarget)
        End If

    End If
End Sub


来源:https://stackoverflow.com/questions/10738161/is-it-possible-to-rearrange-tab-items-in-tab-control-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!