xaml - WPF TreeView HierarchicalDataTemplate - binding to object with multiple child collections -
i trying treeview
bind collection groups show nested groups , each group show entry.
how can use hierarchicaldatatemplate
treeview
process both subgroups , entries collection?
groups show subgroups , entries:
example: group1 --entry --entry group2 --group4 ----group1 ------entry ------entry ----entry ----entry --entry --entry group3 --entry --entry
objects:
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; } } }
test data:
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"}); //grp3 grp3.entries.add(new entry(){ key=7, name = "entry number 7"}); grp3.entries.add(new entry(){ key=8, name = "entry number 8"}); grp3.entries.add(new entry(){ key=9, name = "entry number 9"}); //grp4 grp4.entries.add(new entry(){ key=10, name = "entry number 10"}); grp4.entries.add(new entry(){ key=11, name = "entry number 11"}); grp4.entries.add(new entry(){ key=12, name = "entry number 12"}); grp4.subgroups.add(grp1); grp2.subgroups.add(grp4); groups.add(grp1); groups.add(grp2); groups.add(grp3); } } }
xaml:
<window x:class="drilldownview.window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:taskmanager.domain;assembly=taskmanager.domain" title="window2" height="300" width="300"> <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="auto" /> </grid.columndefinitions> <treeview name="groupview" grid.row="0" grid.column="0" itemssource="{binding}"> <treeview.resources> <hierarchicaldatatemplate datatype="{x:type local:group}" itemssource="{binding subgroups}"> <textblock text="{binding path=name}" /> </hierarchicaldatatemplate> <hierarchicaldatatemplate datatype="{x:type local:entry}" itemssource="{binding entries}"> <textblock text="{binding path=name}" /> </hierarchicaldatatemplate> </treeview.resources> </treeview> </grid> </window>
xaml.cs:
public partial class window2 : window { public window2() { initializecomponent(); loadview(); } private void loadview() { testdata data = new testdata(); data.load(); groupview.itemssource = data.groups; } }
a hierarchicaldatatemplate
way of saying 'this how render type of object , here property can probed find child nodes under object'
therefore need single property returns 'children' of node. e.g. (if can't make both group , entry derive common node type)
public class group{ .... public ilist<object> items { { ilist<object> childnodes = new list<object>(); foreach (var group in this.subgroups) childnodes.add(group); foreach (var entry in this.entries) childnodes.add(entry); return childnodes; } }
next don't need hierdatatemplate
entry since entry doesn't have children. xaml needs changed use new items property , datatemplate
entry:
<treeview name="groupview" grid.row="0" grid.column="0" itemssource="{binding}"> <treeview.resources> <hierarchicaldatatemplate datatype="{x:type local:group}" itemssource="{binding items}"> <textblock text="{binding path=name}" /> </hierarchicaldatatemplate> <datatemplate datatype="{x:type local:entry}" > <textblock text="{binding path=name}" /> </datatemplate> </treeview.resources> </treeview>
and here's looks like.
Comments
Post a Comment