WPF TreeView HierarchicalDataTemplate-绑定到具有多个子集合的对

我试图获取一个TreeView绑定我的收藏集的方法,以便所有组都显示嵌套的组,而每个组都将显示条目。


我该如何使用HierarchicalDataTemplate以便TreeView处理子组和条目集合?


组显示子组和条目:


Example:

Group1

--Entry

--Entry

Group2

--Group4

----Group1

------Entry

------Entry

----Entry

----Entry

--Entry

--Entry

Group3

--Entry

--Entry



对象:

namespace TaskManager.Domain

{

    public class Entry

    {

        public int Key { get; set; }

        public string Name { get; set; }

    }

}


namespace TaskManager.Domain

{

    public class Group

    {

        public int Key { get; set; }

        public string Name { get; set; }


        public IList<Group> SubGroups { get; set; }

        public IList<Entry> Entries { get; set; }

    }

}

测试数据:

namespace DrillDownView

{

    public class TestData

    {


        public IList<Group> Groups = new List<Group>();


        public void Load()

        {

            Group grp1 = new Group() { Key = 1, Name = "Group 1", SubGroups = new List<Group>(), Entries = new List<Entry>() };

            Group grp2 = new Group() { Key = 2, Name = "Group 2", SubGroups = new List<Group>(), Entries = new List<Entry>() };

            Group grp3 = new Group() { Key = 3, Name = "Group 3", SubGroups = new List<Group>(), Entries = new List<Entry>() };

            Group grp4 = new Group() { Key = 4, Name = "Group 4", SubGroups = new List<Group>(), Entries = new List<Entry>() };


            //grp1

            grp1.Entries.Add(new Entry() { Key=1, Name="Entry number 1" });

            grp1.Entries.Add(new Entry() { Key=2, Name="Entry number 2" });

            grp1.Entries.Add(new Entry() { Key=3,Name="Entry number 3" });


            //grp2

            grp2.Entries.Add(new Entry(){ Key=4, Name = "Entry number 4"});

            grp2.Entries.Add(new Entry(){ Key=5, Name = "Entry number 5"});

            grp2.Entries.Add(new Entry(){ Key=6, Name = "Entry number 6"});


        }

    }

}


UYOU
浏览 1336回答 3
3回答

动漫人物

A HierarchicalDataTemplate是一种表达方式,“这就是您呈现这种类型的对象的方式,这是可以探查以找到该对象下的子节点的属性”因此,您需要一个返回该节点“子级”的属性。例如(如果您不能同时使“组”和“条目”都源自同一个节点类型)public class Group{ ....&nbsp; &nbsp; &nbsp; &nbsp; public IList<object> Items&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IList<object> childNodes = new List<object>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var group in this.SubGroups)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; childNodes.Add(group);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var entry in this.Entries)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; childNodes.Add(entry);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return childNodes;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }接下来HierarchicalDataTemplate,由于条目没有子项,因此不需要条目。因此,需要更改XAML以使用新的Items属性和DataTemplatefor条目:<TreeView Name="GroupView" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}">&nbsp; &nbsp; <TreeView.Resources>&nbsp; &nbsp; &nbsp; &nbsp; <HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding Items}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Text="{Binding Path=Name}" />&nbsp; &nbsp; &nbsp; &nbsp; </HierarchicalDataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate DataType="{x:Type local:Entry}" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Text="{Binding Path=Name}" />&nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; </TreeView.Resources></TreeView>

交互式爱情

我认为您已经完成了大部分工作...只需进行一点点返工,就可以轻松完成此工作...我建议您创建一个基本抽象类(或您喜欢的一个接口),并为Group和Entry类继承/实现它。这样,您可以在Group对象中公开一个属性public ObservableCollection<ITreeViewItem> Children { get; set; }^至此,您可以决定是否替换子组和条目的列表,或者只是将它们附加在一起并在属性getter中返回它们...现在,您只需要使用Group或Entry对象填充Children集合,HierarchicalDataTemplate当将对象放置在TreeView中时,它将正确呈现。最后要考虑的是,如果Entry始终是树的“底层”(即没有子级),则无需HierarchicalDataTemplate为Entry对象定义a即可DataTemplate。希望这可以帮助 :)

慕运维8079593

这是Gishu答案的替代实现,它返回IEnumerable而不是IList,并使用yield关键字来简化代码:public class Group{&nbsp; &nbsp; ...&nbsp; &nbsp; public IEnumerable<object> Items&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var group in this.SubGroups)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return group;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var entry in this.Entries)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return entry;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP