c# - Data Binding a Generic Object to WPF TreeView? -
i have xml file deserialized object, , want display object in wpf treeview. have been looking in data bindings wpf treeview have not been able find looking for.
is there way have wpf treeview display object , it's children without knowing in advance object structure looks like?
assuming object looks like:
entity entity b entity c entity c entity b entity d
create hierarchical data template each non leaf entity , data template each leaf entity.
i've found easy accommodate type of mixed hierarchy if have in objects observablecollection (called items) @ each level contains children of type below it.
with setup, templates like:
<!-- entity a--> <hierarchicaldatatemplate datatype="{x:type local:entitya}" itemssource="{binding items, mode=oneway}"> <stackpanel> <!-- content > </stackpanel> </hierarchicaldatatemplate> <!-- entity b --> <hierarchicaldatatemplate datatype="{x:type local:entityb}" itemssource="{binding items, mode=oneway}"> <stackpanel> <!-- content > </stackpanel> </hierarchicaldatatemplate> <!-- entity c --> <datatemplate datatype="{x:type local:entityc}"> <stackpanel> <!-- content > </stackpanel> </datatemplate> <!-- entity d --> <datatemplate datatype="{x:type local:entityd}"> <stackpanel> <!-- content > </stackpanel> </datatemplate>
and tree view binding:
<treeview itemssource="{binding items, mode=oneway}" />
this answer assumes know types of objects dealing with, want handle structure/hierarchy of objects, , if don't know if particular object type going leaf 1 or not, can use hierarchical data templates.
Comments
Post a Comment