How to stop drag drop document in WPF TabControl on desire position?
In DockingManager, the following code example describes you how to stop a drag drop document tab on desire position. For Example, here you can avoid dropping on 0th position. In PreviewDrop event of document container, check the Index of the target item, if the target item is 0, then handle the event.
XAML
<syncfusion:DockingManager Name="dockingmanager" UseDocumentContainer="True"> <ContentControl Content="Document" syncfusion:DockingManager.State="Document" syncfusion:DockingManager.Header="Document1"> </ContentControl> <ContentControl Content="Document" syncfusion:DockingManager.State="Document" syncfusion:DockingManager.Header="Document2"> </ContentControl> <ContentControl Content="Document" syncfusion:DockingManager.State="Document" syncfusion:DockingManager.Header="Document3"> </ContentControl> </syncfusion:DockingManager>
C#
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); (dockingmanager.DocContainer as DocumentContainer).PreviewDrop += MainWindow_PreviewDrop; } void MainWindow_PreviewDrop(object sender, DragEventArgs e) { DocumentTabControl tabcontrol = VisualUtils.FindDescendant(sender as Visual, typeof(DocumentTabControl)) as DocumentTabControl; if (tabcontrol != null) { TabItemExt tabitem = VisualUtils.FindAncestor(e.OriginalSource as Visual, typeof(TabItemExt)) as TabItemExt; if (tabitem != null) { int location = tabcontrol.Items.IndexOf(tabitem); // Check the index position based on our requirement if (location == 0) { // Getting Find Decendant to collapsed the adorner after handling the drop InsertionAdorner adorner = FindDescendant(tabcontrol, typeof(InsertionAdorner)) as InsertionAdorner; if (adorner != null) { adorner.Visibility = Visibility.Collapsed; (sender as DocumentContainer).ShowDragAdorner = false; } e.Handled = true; } } } } public static Visual FindDescendant(Visual startingFrom, Type typeDescendant) { Visual visual = null; bool result = false; int iCount = VisualTreeHelper.GetChildrenCount(startingFrom); for (int i = 0; i < iCount; ++i) { Visual child = VisualTreeHelper.GetChild(startingFrom, i) as Visual; if (typeDescendant.IsInstanceOfType(child) && (child as UIElement) != null && (child as UIElement).Visibility==Visibility.Visible) { visual = child; result = true; } if (!result) { if (child != null) { visual = FindDescendant(child, typeDescendant); if (visual != null) { break; } } } else { break; } } return visual; } }
In DockingManager, the following code example describes you how to stop a drag drop document tab on desire position. For Example, here you can avoid dropping on 0th position. In PreviewDrop event of document container, check the Index of the target item, if the target item is 0, then handle the event.
XAML
<syncfusion:DockingManager Name="dockingmanager" UseDocumentContainer="True"> <ContentControl Content="Document" syncfusion:DockingManager.State="Document" syncfusion:DockingManager.Header="Document1"> </ContentControl> <ContentControl Content="Document" syncfusion:DockingManager.State="Document" syncfusion:DockingManager.Header="Document2"> </ContentControl> <ContentControl Content="Document" syncfusion:DockingManager.State="Document" syncfusion:DockingManager.Header="Document3"> </ContentControl> </syncfusion:DockingManager>
C#
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); (dockingmanager.DocContainer as DocumentContainer).PreviewDrop += MainWindow_PreviewDrop; } void MainWindow_PreviewDrop(object sender, DragEventArgs e) { DocumentTabControl tabcontrol = VisualUtils.FindDescendant(sender as Visual, typeof(DocumentTabControl)) as DocumentTabControl; if (tabcontrol != null) { TabItemExt tabitem = VisualUtils.FindAncestor(e.OriginalSource as Visual, typeof(TabItemExt)) as TabItemExt; if (tabitem != null) { int location = tabcontrol.Items.IndexOf(tabitem); // Check the index position based on our requirement if (location == 0) { // Getting Find Decendant to collapsed the adorner after handling the drop InsertionAdorner adorner = FindDescendant(tabcontrol, typeof(InsertionAdorner)) as InsertionAdorner; if (adorner != null) { adorner.Visibility = Visibility.Collapsed; (sender as DocumentContainer).ShowDragAdorner = false; } e.Handled = true; } } } } public static Visual FindDescendant(Visual startingFrom, Type typeDescendant) { Visual visual = null; bool result = false; int iCount = VisualTreeHelper.GetChildrenCount(startingFrom); for (int i = 0; i < iCount; ++i) { Visual child = VisualTreeHelper.GetChild(startingFrom, i) as Visual; if (typeDescendant.IsInstanceOfType(child) && (child as UIElement) != null && (child as UIElement).Visibility==Visibility.Visible) { visual = child; result = true; } if (!result) { if (child != null) { visual = FindDescendant(child, typeDescendant); if (visual != null) { break; } } } else { break; } } return visual; } }
Conclusion
I hope you enjoyed learning about how to stop drag drop a document to specified position.
You can refer to our WPF TabControl feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF TabControl example to understand how to create and manipulate data.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!