【问题标题】:TreeView with CompositeCollection, expand arrow not showing具有 CompositeCollection 的 TreeView,展开箭头未显示
【发布时间】:2013-04-30 16:37:54
【问题描述】:

我有一个 TreeView,它绑定到由 MyItem 和 MyGroup 对象组成的 CompositeCollection。

类定义:

public class MyItem
{
    public string Name { get; set; }

    public MyItem(string name = "")
    {
        Name = name;
    }
}


public class MyGroup
{
    public string Name { get; set; }
    public List<MyGroup> MyGroups = new List<MyGroup>();
    public List<MyItem> MyItems = new List<MyItem>();

    public IList Children
    {
        get
        {
            return new CompositeCollection()
            {
                new CollectionContainer { Collection = MyGroups },
                new CollectionContainer { Collection = MyItems }
            };
        }
    }

    public MyGroup(string name)
    {
        Name = name;
    }
}

XAML:

<TreeView Name="myTreeView">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:MyGroup}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:MyItem}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

设置树的代码:

var root = new ObservableCollection<MyGroup>();
myTreeView.ItemsSource = root;

MyGroup g1 = new MyGroup("First");
MyGroup g2 = new MyGroup("Second");
MyGroup g3 = new MyGroup("Third");
MyItem i1 = new MyItem("Item1");
MyItem i2 = new MyItem("Item2");
MyItem i3 = new MyItem("Item3");

root.Add(g1);
root.Add(g2);
g2.MyGroups.Add(g3);
g1.MyItems.Add(i1);

问题是每当我运行代码时,只显示First和Second,但Second旁边没有展开箭头,并且无法展开。调试显示 g2 有 g3 作为子级,但它不存在于 TreeView 控件中。

我该如何解决?目的是用尽可能少的代码来完成它,我尽量避免添加大量的抽象层和包装类......

经历了这些,并没有解决我的问题:

【问题讨论】:

    标签: c# wpf treeview compositecollection


    【解决方案1】:

    通过对源代码进行以下修改,我能够解决问题:

    班级MyGroup:

    public class MyGroup
    {
        public string Name { get; set; }
    
        private IList children = new CompositeCollection() {
                    new CollectionContainer { Collection = new List<MyGroup>() },
                    new CollectionContainer { Collection = new List<TestItem>() }
        };
    
        public IList Children
        {
            get { return children; }
            set { children = value; }
        }
    
        public MyGroup(string name)
        {
            Name = name;
        }
    }
    

    设置树的代码:

    var root = new ObservableCollection<MyGroup>();
    myTreeView.ItemsSource = root;
    
    MyGroup g1 = new MyGroup("First");
    MyGroup g2 = new MyGroup("Second");
    MyGroup g3 = new MyGroup("Third");
    MyItem i1 = new MyItem("Item1");
    MyItem i2 = new MyItem("Item2");
    MyItem i3 = new MyItem("Item3");
    
    root.Add(g1);
    root.Add(g2);
    g2.Children.Add(g3);
    g1.Children.Add(i1);
    

    因此,尽管WPF Treeview Databinding Hierarchal Data with mixed types 提出了建议,但我必须摆脱 MyGroup 中的两个单独列表(代表不同类型的对象),只使用一个 CompositeCollection,然后将子项添加到子对象列表,与子对象的类型无关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      相关资源
      最近更新 更多