查找TreeViewItem删除数据 [英] Find TreeViewItem to drop data
问题描述
我想从ListView中拖动数据并将其放在TreeView中(可以正常工作)。我使用DataBinding和ItemTemplate填充TreeView。
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 参与者
从ListView拖到TreeView。现在我需要找到组
。任何想法从TreeView中获得正确的组
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?
推荐答案
我只需在HierarchicalDataTemplate的ItemTemplate中的StackPanel上设置Drop =tvDrop和DragOver =tvDragOver。
I would simply set the Drop="tvDrop" and DragOver="tvDragOver" on the StackPanel in the HierarchicalDataTemplate's ItemTemplate.
这样
1)没有任何风险的事件从某个组件中删除
2)您可以安全地将发件人投递到FrameworkElement并获取DataContext并将其转换到您的类。
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.
这篇关于查找TreeViewItem删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!