WPF TreeView HierarchicalDataTemplate Drag and Drop

I have a tree in wpf that is built using xaml below. This is a well-structured data source and I have a lot of drag and drop issues. I tried several methods, all to no avail. Can someone tell me what is the standard procedure for this type?

<TreeView x:Name="_treeView" ItemsSource="{Binding}" Grid.Row="0" Grid.Column="0"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type Logic:Statement}" ItemsSource="{Binding Path=PagedChildren}"> <TextBlock Text="{Binding StatementName}"/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type Logic:StatementPage}" ItemsSource="{Binding Path=Children}"> <WrapPanel> <TextBlock Text="Page: "/> <TextBlock Text="{Binding PageIndex}"/> </WrapPanel> </HierarchicalDataTemplate> <DataTemplate DataType="{x:Type Logic:StatementFund}"> <Border HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="2" CornerRadius="25"> <WrapPanel Margin="30 0 30 0" Width="150" Height="150" > <StackPanel> <TextBlock Text="Fund"/> <WrapPanel> <TextBlock Text="Fund: "/> <TextBlock Text="{Binding FundNumber}"/> </WrapPanel> <WrapPanel Margin="10 0 0 0"> <TextBlock Text="{Binding ColumnIndex}"/> </WrapPanel> </StackPanel> </WrapPanel> </Border> </DataTemplate> <DataTemplate DataType="{x:Type Logic:StatementPreviousCycle}"> <Border HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="2" CornerRadius="25"> <WrapPanel Margin="30 0 30 0" Width="150" Height="150" > <StackPanel> <TextBlock Text="Previous Cycle"/> <WrapPanel> <TextBlock Text="Fund: "/> <TextBlock Text="{Binding FundNumber}"/> </WrapPanel> <WrapPanel Margin="10 0 0 0"> <TextBlock Text="{Binding ColumnIndex}"/> </WrapPanel> </StackPanel> </WrapPanel> </Border> </DataTemplate> </TreeView.Resources> </TreeView> 
+4
source share
1 answer

I use the methods on this site for general drag and drop.

the tree view can become messy if you want to know which node you are preivewMouseDown and then use it as a drag and drop element, you can end up going through the visual tree. there is code for this here . another way is to subclass treeview and treeviewitem, then you can redefine the preview mouse on each element of the tree view and tell about your parent tree view about it, which can indicate the element of the view tree as the selected element.

+4
source

All Articles