私はこの回答をmy previous questionに送ろうと思っていましたが、それは役に立ちませんでした。初期の状況はほとんど同じです:wpf-treeviewで親項目を見つける
<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;
しかし、これは動作しませんでした:私はこの1つのようなものを試してみました。 Group
は、Participant
(btnDeleteParticipants.Tag
にバインドされています)が含まれているかどうかを調べることで確認できますが、参加者は複数のグループに属しません。 だから、どのように正しい方法を得るためにどのようなアイデアGroup
?
編集:
Groups = new ObservableCollection<Group>();
Participants = new ObservableCollection<Participant>();
にあなたは面白いジョシュ・スミスによって[この記事](http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx)かもしれません。 – mostlytech