【发布时间】:2009-03-23 17:42:10
【问题描述】:
我希望my previous question 的答案能帮助我解决这个问题,但没有。最初的情况几乎相同:
<TreeView ItemsSource="{Binding Groups}" Name="tvGroups" AllowDrop="True">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Participants}">
<StackPanel Orientation="Horizontal" Drop="tvDrop" Tag="{Binding .}">
<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 btnParticipants_Click(object sender, RoutedEventArgs e)//Probanten aus Gruppe entfernen
{
Participant p = ((sender as Button).Tag as Participant);
if (p == null) return;
//TODO: Raus bekommen in welcher Gruppe ich löschen will
}
我想通过单击按钮 (btnDeleteParticipants) 从 Group 中删除 Participant p。我试过这样的:
Control c = sender as Control;
while (!(c is TreeViewItem))
c = (c.Parent) as Control;
但这不起作用(不要问为什么,我不确定)。我可以通过检查它是否包含Participant(绑定到btnDeleteParticipants.Tag)来找到Group,但这将不允许参与者加入超过1个组。
那么,任何想法如何获得正确的Group?
编辑:
Groups = new ObservableCollection<Group>();
Participants = new ObservableCollection<Participant>();
【问题讨论】:
-
你可能会觉得 Josh Smith 的 this article 很有趣。
标签: c# wpf data-binding xaml