I hoped the answer to my previous question whould help me with this one, but it didn’t. The initial situation is pretty much the same:
<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 }
I want to remove Participant p from a Group by clicking the button(btnDeleteParticipants). I tried something like this one:
Control c = sender as Control; while (!(c is TreeViewItem)) c = (c.Parent) as Control;
But this didn’t work (don’t ask why, I’m not sure). I could find the Group by checking if it contains the Participant(bound to btnDeleteParticipants.Tag), but this would disallow participants to be in more than 1 group. So, any ideas how to get the right Group?
Edit:
Groups = new ObservableCollection<Group>(); Participants = new ObservableCollection<Participant>();
Are Groups and Participants ObservableCollection objects?
Try using this:
Also, try using the DataContext to get the participant and set the Tag to the TemplatedParent.
Then on click