WPF - 绑定的TreeView不更新根项目 [英] WPF - Bound treeview not updating root items

查看:292
本文介绍了WPF - 绑定的TreeView不更新根项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF TreeView控件,我已经绑定到基于ObservableCollections一个简单的树形结构。这里的XAML:

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


<TreeView Name="tree" Grid.Row="0"> 
    <TreeView.ItemTemplate> 
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}"> 
            <TextBlock Text="{Binding Path=Text}"/> 
        </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 
</TreeView>  

和树型结构:


public class Node : IEnumerable { 
    private string text; 
    private ObservableCollection<Node> children; 
    public string Text { get { return text; } } 
    public ObservableCollection<Node> Children { get { return children; } } 
    public Node(string text, params string[] items){ 
        this.text = text; 
        children = new ObservableCollection<Node>(); 
        foreach (string item in items) 
            children.Add(new Node(item)); 
    } 
    public IEnumerator GetEnumerator() { 
        for (int i = 0; i < children.Count; i++) 
            yield return children[i]; 
    } 
} 

我设置的ItemsSource此树是我的树结构的根,而那成为root级别的项目在树(就像我想要的)孩子们:

I set the ItemsSource of this tree to be the root of my tree structure, and the children of that become root-level items in the tree (just as I want):


private Node root; 

root = new Node("Animals"); 
for(int i=0;i<3;i++) 
    root.Children.Add(new Node("Mammals", "Dogs", "Bears")); 
tree.ItemsSource = root; 

我可以添加新的孩子我的树结构的各种非根节点,它们出现在TreeView权,他们应该。

I can add new children to the various non-root nodes of my tree structure, and they appear in the TreeView right where they should.

root.Children[0].Children.Add(new Node("Cats", "Lions", "Tigers"));  

但是,如果我的子添加到根节点:

But, if I add a child to the root node:

root.Children.Add(new Node("Lizards", "Skinks", "Geckos")); 

该项目不会出现,并没有什么我试过(如设置的ItemsSource为null,然后再返回),导致其出现。

The item does not appear, and nothing I've tried (such as setting the ItemsSource to null and then back again) has caused it to appear.

如果我设置的ItemsSource之前添加蜥蜴,他们出现,但如果我加入他们算账。

If I add the lizards before setting the ItemsSource, they show up, but not if I add them afterwards.

任何想法?

推荐答案

您要设置的ItemsSource =根恰好实现IEnumerable,但不是其本身观察到。即使你有一个孩子属性,它是可观的,这不是你绑定什么树视图,所以该树视图没有听通过Children属性所发生的变化的任何方式。

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.

我会完全放弃从Node类的IEnumerable。然后设置treeView.ItemsSource = root.Children;

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

这篇关于WPF - 绑定的TreeView不更新根项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆