c# - Different types of treeview items to be binded in one single data template -
i have mvvm based usercontrol. model has 2 classes, 1 class has list of type class shown in model below. problem i have 2 define 2 different hierarchicaldatatemplates treeview binded model wrong. make 1 hierarchicaldatatemplate know property in viewmodel. in end, want treeview like:
|familyname
||personname
||personname
the code quite big, have taken pieces. if more info required please let me know:
model
public class family { private string m_name; public string nameoffamily { { return m_name; } set { m_name= value; } } public observablecollection<person> persons{ get; set ; } } public class person { private string m_name; public string nameofperson { { return m_name; } set { m_name= value; } } }
in viewmodel received family respective persons e.g.
viewmodel
family fam = getfamilywithrespectivepersons;
view
<treeview name="mytreeview" grid.column="0" width="auto" height="300" itemssource="{binding fam}"> <treeview.resources> <hierarchicaldatatemplate datatype="{x:type classes:family}" itemssource="{binding persons}"> <treeviewitem header="{binding nameoffamily}"></treeviewitem> </hierarchicaldatatemplate> <hierarchicaldatatemplate datatype="{x:type classes:person}"> <treeviewitem header="{binding nameofperson}"></treeviewitem> </hierarchicaldatatemplate> </treeview.resources> </treeview>
it not wrong define datatemplate
per type. since person
class has no children, datatemplate
doesn't have hierarchical.
also, templates shouldn't include treeviewitem
elements:
<treeview name="mytreeview" grid.column="0" width="auto" height="300" itemssource="{binding fam}"> <treeview.resources> <hierarchicaldatatemplate datatype="{x:type classes:family}" itemssource="{binding persons}"> <textblock text="{binding nameoffamily}" /> </hierarchicaldatatemplate> <datatemplate datatype="{x:type classes:person}"> <textblock text="{binding nameofperson}" /> </datatemplate> </treeview.resources> </treeview>
Comments
Post a Comment