WPF DataBound treeview expand / collapse WPF DataBound treeview expand / collapse wpf wpf

WPF DataBound treeview expand / collapse


Came up with solution. Really simple:

    <Style TargetType="{x:Type TreeViewItem}">        <Setter Property="IsExpanded" Value="{Binding IsNodeExpanded}">        </Setter>    </Style>

So the style gets the object bound to the TreeViewItem and looks at its IsNodeExpanded attribute and it assigns that value to the TreeViewItem.IsExpanded property. If you add Mode=TwoWay, they'll notify each other (TreeViewItem will tell the object when it has been expanded).

Thanks!


FWIW, you may be interested in this CodeProject article by Josh Smith which shows how to create a MVVM based tree view using a generic (n-level) approach.

I'm not suggesting there is anything wrong with Carlo's implementation, but I found that article helpful in understanding the TreeView control, and MVVM in general.