[UWP] [TreeView - Windows UI库]通过ItemsSource属性添加子项时不起作用。 [英] [UWP] [TreeView - Windows UI Library] Not working when add child through ItemsSource property.
问题描述
   大家好!
Hi people!
    我需要在我的应用程序中创建一个树导航,然后我去了TreeView控件。但是当一个子项被添加到ObservableCollection时,我没有让它重绘(Binding),这适用于添加/删除根节点的
。我已经尝试了所有文档示例,但没有成功,那会是一个错误吗?
b
$
I need to create a tree navigation in my application and with that, I went behind a TreeView control. But I'm not getting it to redraw (Binding) when a child is added to the ObservableCollection, this works fine with Add/Remove
of root nodes. I've tried all the documentation example, but without success, would that be a bug?
[XAML]
[XAML]
<controls:TreeView
Width="345"
MaxHeight="500"
Margin="0,12,0,0"
VerticalAlignment="Top"
HorizontalAlignment="Center"
ItemsSource="{x:Bind FolderList, Mode=OneWay}"
ItemTemplate="{StaticResource TreeViewItemDataTemplate}">
[代码]
[Code]
public ObservableCollection<TreeViewNode> FolderList { get; set; } = new ObservableCollection<TreeViewNode>();
FolderList.First().Children.Add(new TreeViewNode { Content = "Child" });
当我添加孩子时,FolderList不为空。
The FolderList is not empty when I'm adding a child.
关注链接文档:
https://docs.microsoft.com/en-us/windows / uwp / design / controls-and-patterns / tree-view
https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/tree-view
谢谢,每个人!
推荐答案
问题是TreeViewNode没有自动实现 INO tifyPropertyChanged Interface 。 ObservableCollection auto 实现
接口,但它只影响TreeViewNode对象。但是TreeViewNode中的更改不会由ObservableCollection处理。因此,您可能需要创建一个继承自TreeViewNode类的自定义类,并实现 INotifyPropertyChanged
界面 。那时,如果您将一个子项添加到TreeViewNode,TreeView将会更新。
The problem is that the TreeViewNode does not auto implement the INotifyPropertyChanged Interface . The ObservableCollection auto implements the interface but it only affects the TreeViewNode object. But changes inside the TreeViewNode wont be handled by the ObservableCollection. So you might need to create a customized class inherited from the TreeViewNode class and implement the INotifyPropertyChanged Interface . At that time, if you add a child to the TreeViewNode, the TreeView will be updated.
您可以在此处获得有关数据绑定的更多信息:深度数据绑定。
You could get more information about data binding here:Data binding in depth.
祝你好运,
Roy
这篇关于[UWP] [TreeView - Windows UI库]通过ItemsSource属性添加子项时不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!