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

后端 未结 2 1072
情书的邮戳
情书的邮戳 2021-02-04 09:39

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

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-04 10:09

    found a solution in the MSDN forum.

    Here is the link:

    DragDrop TabItem

    Here is the solution:

    C# solution

    WPF code:

    
        
            
        
    
        
        
        
        
        
    
    

    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;
        }
    }
    

提交回复
热议问题