是否有可能重新安排在运行时标签控件标签的物品? 比如我有一个是关于汽车和房屋约4片3个项目。 我希望能够使用拖放来重新排列。 是否有可能或很奇妙的东西?
我这里有选项卡控件是XAML。
<TabControl x:Name="tc" Visibility="Collapsed" GotFocus="Focus" AllowDrop="True" >
</TabControl>
标签项目将在运行系统中添加。 感谢您帮助我!
是否有可能重新安排在运行时标签控件标签的物品? 比如我有一个是关于汽车和房屋约4片3个项目。 我希望能够使用拖放来重新排列。 是否有可能或很奇妙的东西?
我这里有选项卡控件是XAML。
<TabControl x:Name="tc" Visibility="Collapsed" GotFocus="Focus" AllowDrop="True" >
</TabControl>
标签项目将在运行系统中添加。 感谢您帮助我!
发现在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);
}
}
当我试图实施该解决方案,下降事件发射两次(移动标签,但他们立即将它们搬回)。 我不得不添加一个整数跟踪最后一个选项卡的目标指数。 我的解决办法是在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