I have a treeview with the following structure:
Attribute1
--Value11
--Value12
Attribute2
--Value21
--...
...
The treeview is populated with an HierarchicalDataTemplate
of custom classes Attribute
and Value
.
Now I need to get the parent (attribute) of the currently selected item (value).
What I tried:
DependencyObject obj = treeViewAttributes.ItemContainerGenerator.ContainerFromItem(treeViewAttributes.Items.CurrentItem);
DependencyObject parentNode = ((TreeViewItem)obj).Parent;
Attribute parentAttribute = treeViewAttributes.ItemContainerGenerator.ItemFromContainer(obj) as AttributeType;
However the first line doesn't retrieve the selected object, but the root node. And ContainerFromItem(treeViewAttributes.SelectedItem)
returns null
.