WPF DataBound treeview expand / collapse

后端 未结 2 2061
北荒
北荒 2020-12-09 16:25

I\'m just trying to find a way to control the expand / collapse of the TreeView nodes through the object they\'re bound to. The object has an IsExpanded

相关标签:
2条回答
  • 2020-12-09 17:14

    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!

    0 讨论(0)
  • 2020-12-09 17:14

    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.

    0 讨论(0)
提交回复
热议问题