c# - wpf mvvm recursive tree -
i have 2 classes
public class leaf { public string name { get; set; } }
and
public class node { public observablecollection<node> nodelist = new observablecollection<node>(); public observablecollection<leaf> leaflist = new observablecollection<leaf>(); public observablecollection<node> prop_nodelist { { return nodelist; } set { nodelist = value; } } public observablecollection<leaf> prop_leaflist { { return leaflist; } set { leaflist = value; } } public string name { get; set; } }
as can see work roads or tree. node can have information on node , leaf.
i show in user control tree formated that
nethead main node, , have 2 node (b, a). node have 2 node(b,c) , 2 leaf (a1, a2). dont mvvm. when mvvm like
from c# im doing only
this.datacontext = mainnode; // node hold everething (its named nethead)
from xaml its
<grid> <treeview itemssource="{binding prop_nodelist}"> <treeview.resources> <hierarchicaldatatemplate datatype="{x:type local:node}" itemssource="{binding prop_nodelist}"> <textblock text="{binding path=name}"/> </hierarchicaldatatemplate> <hierarchicaldatatemplate datatype="{x:type local:leaf}" itemssource="{binding prop_leaflist}"> <textblock text="{binding path=name}"/> </hierarchicaldatatemplate> </treeview.resources> </treeview> </grid>
you see want ? want make double treeitem in treeview, doesnt work :( please help, projekt ant algorithm , want best gui in class room
have bit modified code: implemented root view model nodesdata demonstrate , create startup data. added property children have single property children of node. , modified xaml use single hierarchical data template. , seems works.
public class leaf { public string name { get; set; } } public class node { public string name { get; set; } public observablecollection<node> nodelist = new observablecollection<node>(); public observablecollection<leaf> leaflist = new observablecollection<leaf>(); public observablecollection<node> prop_nodelist { { return nodelist; } set { nodelist = value; } } public observablecollection<leaf> prop_leaflist { { return leaflist; } set { leaflist = value; } } public observablecollection<object> children { { var children = prop_nodelist.oftype<object>(); return new observablecollection<object>(children.concat(prop_leaflist.oftype<object>())); } } } public class nodesdata { public observablecollection<node> rootsource { get; set; } public nodesdata() { var rootnode = new node() { name = ">>>head node<<<<" }; var b = new node() { name = "b" }; var c = new node() { name = "c" }; var = new node() { name = "a", prop_nodelist = new observablecollection<node>() { b, c }, prop_leaflist = new observablecollection<leaf>() { new leaf() { name = "a1" }, new leaf() { name = "a2" } } }; rootnode.prop_nodelist = new observablecollection<node>() { b, }; rootsource = new observablecollection<node>() { rootnode }; } }
}
xaml:
<grid> <grid.resources> <wpfapplication1:nodesdata x:key="datasource"/> </grid.resources> <treeview itemssource="{binding rootsource}" datacontext="{staticresource datasource}"> <treeview.resources> <hierarchicaldatatemplate datatype="{x:type wpfapplication1:node}" itemssource="{binding children}"> <textblock text="{binding path=name}"/> </hierarchicaldatatemplate> <datatemplate datatype="{x:type wpfapplication1:leaf}" > <textblock text="{binding path=name}"/> </datatemplate> </treeview.resources> </treeview> </grid>
Comments
Post a Comment