Reputation: 15441
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?
Upvotes: 1
Views: 4806
Reputation: 292405
You shouldn't put a TreeViewItem
in your DataTemplate
. The TreeViewItem
is created automatically by the TreeView
. The DataTemplate
defines the content of the TreeViewItem
, not the TreeViewItem
itself. Now you have two nested TreeViewItems
, which is probably why it doesn't work as expected. Try that instead:
<HierarchicalDataTemplate ItemsSource="{Binding Path=TreeNodes, Mode=TwoWay}" DataType="{x:Type TestExplorer:FolderTreeNode}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
Upvotes: 6
Reputation: 263
Try TreeViewItem.Selected
Event instead of the TreeView.SelectedItemChanged
Upvotes: 1