I want to drag data from a ListView and drop it in a TreeView(the draging works fine). I use DataBinding and ItemTemplate to fill the TreeView.
<TreeView ItemsSource='{Binding Groups}' Name='tvGroups' AllowDrop='True' Drop='tvDrop' DragOver='tvDragOver'> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource='{Binding Participants}'> <StackPanel Orientation='Horizontal'> <TextBlock Text='{Binding Name}' /> <Button Tag='{Binding .}' Click='Button_Click_2'> <Image Source='Resources/cross.png' /> </Button> </StackPanel> <HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <StackPanel Orientation='Horizontal' > <TextBlock Text='{Binding Alias}' /> <Button Tag='{Binding .}' Name='btnDeleteParticipants' Click='btnParticipants_Click' > <Image Source='Resources/cross.png' /> </Button> </StackPanel> </DataTemplate> </HierarchicalDataTemplate.ItemTemplate> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
private void tvDrop(object sender, DragEventArgs e) { if (e.Effects == DragDropEffects.Copy && e.Data.GetDataPresent(typeof(Participant))) { Participant data = e.Data.GetData(typeof(Participant)) as Participant; } }
A Participant is dragged from the ListView to the TreeView. Now I need to find the Group. Any ideas where to get the right Group from the TreeView?
I would simply set the Drop=’tvDrop’ and DragOver=’tvDragOver’ on the StackPanel in the HierarchicalDataTemplate’s ItemTemplate.
This way 1) You don’t have any risk of getting an event when something is dropped out of a group 2) You can safely cast the Sender to a FrameworkElement and get the DataContext and cast it to your class.
You can also set a different handler on the treeview itself if you need to support dragging out of the groups.