I have an XML file I deserialized into an object, and now I want to display that object in a WPF TreeView. I have been looking in to Data Bindings with the WPF TreeView but I have not been able to find what I am looking for.
Is there a way to have the WPF TreeView display an object and it’s children without knowing in advance what the object structure looks like?
Assuming your object looks something like:
Create a hierarchical data template for each non leaf entity and a data template for each leaf entity.
I’ve found it easy to accommodate any type of mixed hierarchy if you have in your objects an ObservableCollection (called something like Items) at each level that contains the children of any type below it.
With this setup, the templates would look something like:
And the tree view binding:
This answer assumes you know the types of objects you are dealing with, but want to handle any structure/hierarchy of those objects, and if you don’t know if a particular object type is going to be a leaf one or not, you can always use hierarchical data templates.