tìm thấy giải pháp trong diễn đàn MSDN.
Dưới đây là liên kết:
DragDrop TabItem
Dưới đây là giải pháp:
C# giải pháp mã
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>
Mã C# đằng sau:
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);
}
}
Bạn có thể vui lòng cho tôi mã trong C#, nó sẽ được đánh giá cao. Cảm ơn bạn. –
Mã hiện đang ở C#. Giải pháp này làm việc cho tôi. – csteinmueller
Mã này hoạt động tốt khi Mục tab trống, nhưng mục tab của tôi có Điều khiển người dùng trong đó có nhiều yếu tố giao diện người dùng. Thậm chí tôi có thể đặt con trỏ vào TextBox, nó cho tôi lỗi. Nhấp chuột đang hoạt động trong từng yếu tố giao diện người dùng không phải là thứ tôi muốn. tôi hy vọng tôi có thể giải thích cho bạn. –