是否可以在wpf中重新排列选项卡控件中的选项卡项?

时间:2012-05-24 13:10:38

标签: wpf wpf-controls wpftoolkit

是否可以在运行时重新排列选项卡控件中的选项卡项?例如,我有3个关于汽车的标签项目和关于房子的4个标签。我希望能够使用拖放重新排列它们。它有可能还是它太棒了?

我有Tab Control这里是XAML。

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

选项卡项目将在运行时添加。 谢谢你的帮助!

2 个答案:

答案 0 :(得分:23)

在MSDN论坛中找到了解决方案。

这是链接:

DragDrop TabItem

以下是解决方案:

C#解决方案

WPF代码:

<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#代码背后:

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

答案 1 :(得分:0)

当我尝试实现此解决方案时,drop事件触发两次(移动选项卡,但它们立即将它们移回)。我必须添加一个整数以跟踪最后一个制表符目标索引。我的解决方案是在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