WPF - Bound treeview not updating root items

后端 未结 2 1900
没有蜡笔的小新
没有蜡笔的小新 2021-01-24 10:31

I\'m using a WPF TreeView control, which I\'ve bound to a simple tree structure based on ObservableCollections. Here\'s the XAML:




        
相关标签:
2条回答
  • 2021-01-24 10:59

    You are setting ItemsSource = root which happens to implement IEnumerable but is not in and of itself observable. Even though you have a Children property which is observable, that's not what you're binding the TreeView to so the TreeView doesn't have any way of listening to changes that occur through the Children property.

    I would drop IEnumerable from the Node class altogether. Then set treeView.ItemsSource = root.Children;

    0 讨论(0)
  • 2021-01-24 11:16

    if 'root' is an ObservableCollection your treeview will update. is 'root' an observable collection, or is root a node that is in an observable collection? seeing your binding for the items source would help to answer this question. as you are assigning it in code you might just be setting it to be a single element, not a collection

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