0

I am using hierarchical data to populate tree view. Clicking the triangle icon of the root node would expand/collapse child nodes. I want this behavior: When I click anywhere on the root node, it should expand/collapse the child nodes. It should toggle between expand/collapse.

How can i achieve this? Thanks in advance.

tom
  • 75
  • 1
  • 10

1 Answers1

2

Try this

<TreeView TreeViewItem.Selected="TreeViewItem_Selected"/>

and in the xaml.cs

private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
{
   TreeViewItem tvi = (TreeViewItem)e.OriginalSource;
   if (tvi != null)
      tvi.IsExpanded = !tvi.IsExpanded;
}

Reference : WPF expand TreeView on single mouse click and How to expand WPF TreeView on single click of item

Community
  • 1
  • 1
  • It doesn't toggle between expand/collapse if i select the same node. – tom Mar 25 '14 at 21:01
  • if i select the same node means..? check this http://social.msdn.microsoft.com/Forums/vstudio/en-US/c1b15925-75ec-4142-bd0a-b8ca75cfd901/treeview-single-click-expansion-disable-double-click?forum=wpf – NullReferenceException Mar 26 '14 at 03:32