I think my question is rather basic, but I can’t find an answer:
I have a TreeView, and I can’t figure out how to monitor nodes’ selection. I have breakpoints at the handlers for both TreeView.SelectedItemChanged and TreeViewItem.Expanded. When a node is expanded I do see the TreeViewItem.Expanded handler at work, but when clicking on a node, none of those events are fired.
What am I doing wrong?
Thanks.
TreeView code:
<TreeView Name="TestsTree" Height="Auto" MinHeight="50" ItemsSource="{Binding Path=TreeNodes, Mode=TwoWay}" TreeViewItem.Expanded="TestsTreeExpanded" TreeView.SelectedItemChanged="TestsTreeSelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=TreeNodes, Mode=TwoWay}" DataType="{x:Type TestExplorer:FolderTreeNode}">
<TreeViewItem Header="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Edit:
I’ve tried using TreeViewItem.Selected but it’s the same – when I click on a node’s name there’s no reaction, though when I click at the expansion area, both TreeViewItem.Selected and TreeViewItem.Selected are fired. Any explanation?
You shouldn’t put a
TreeViewItemin yourDataTemplate. TheTreeViewItemis created automatically by theTreeView. TheDataTemplatedefines the content of theTreeViewItem, not theTreeViewItemitself. Now you have two nestedTreeViewItems, which is probably why it doesn’t work as expected. Try that instead: