To store an object (say, an instance of a class) in a TreeViewItem, I am currently storing the object in the TreeViewItem‘s Header and then overriding the ToString method of that class, so that it displays the correct string header; I then cast the object back during an event.
Is this the correct way to achieve this sort of thing, or is there any better, more proper way ?
The “proper” way is to just add the
objectto theTreeView‘s (orTreeViewItem‘s)Itemscollection and use aHierarchicalDataTemplateto control how the item is rendered:Person.cs:
Window1.xaml.cs:
Window1.xaml: