このソリューションを実装しようとすると、dropイベントが2回発生していました(タブは移動しましたが、すぐに元に戻りました)。最後のタブターゲットインデックスを追跡するために整数を追加する必要がありました。私の解決策は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