在运行时重新排列选项卡控件中的选项卡项目是否可能?例如,我有3个关于汽车的选项卡和4个关于房屋的选项卡。我想使用拖放功能重新排列它们。这是可能的还是不可想象的?
我这里有一个 XAML 的选项卡控件。
<TabControl x:Name="tc" Visibility="Collapsed" GotFocus="Focus" AllowDrop="True" >
</TabControl>
标签项将在运行时添加。感谢您的帮助!
在运行时重新排列选项卡控件中的选项卡项目是否可能?例如,我有3个关于汽车的选项卡和4个关于房屋的选项卡。我想使用拖放功能重新排列它们。这是可能的还是不可想象的?
我这里有一个 XAML 的选项卡控件。
<TabControl x:Name="tc" Visibility="Collapsed" GotFocus="Focus" AllowDrop="True" >
</TabControl>
标签项将在运行时添加。感谢您的帮助!
我在MSDN论坛中找到了解决方案。
这是链接:
以下为解决方案:
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)
{
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;
}
}
当我尝试实现这个解决方案时,放置事件会触发两次(移动选项卡,然后立即将它们移回)。我不得不添加一个整数来跟踪上一个选项卡目标索引。我的解决方案是使用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
if (!tabItemTarget.Equals(tabItemSource))
处收到了错误信息"对象引用未设置为对象的实例。" - Jason Axelrod!(e.OriginalSource is Button)
。 - bytecode77