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 overri
The "proper" way is to just add the object to the TreeView's (or TreeViewItem's) Items collection and use a HierarchicalDataTemplate to control how the item is rendered:
Person.cs:
public class Person
{
private readonly ICollection _children = new ObservableCollection();
public string Name { get; set; }
public ICollection Children
{
get
{
return _children;
}
}
}
Window1.xaml.cs:
public Window1()
{
InitializeComponent();
var people = new List();
var kent = new Person() { Name = "Kent" };
kent.Children.Add(new Person() { Name = "Tempany" });
people.Add(kent);
_treeView.ItemsSource = people;
}
Window1.xaml: