I have a hierarchical data classes like
public class MyNode
{
public string Name { get; set;}
public bool IsExpanded { get; set;}
public List<MyNode> Nodes { get; set;}
}
I could define a HierarchicalDataTemplate to bind MyNode classes to the TreeView.
<sdk:TreeView ItemsSource="{Binding RootNodes}">
<sdk:TreeView.ItemTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
<TextBlock Text="{Binding Name}" />
</sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>
</sdk:TreeView>
The question is how to data-bind IsExpanded property of TreeViewItem to corresponding MyNode.IsExpanded property, so I could persist this information.
Thank you in advance, Lex
It is possible, if we override TreeView & TreeViewItem classes like this:
public class ExTreeView : System.Windows.Controls.TreeView
{
protected override DependencyObject GetContainerForItemOverride()
{
return new ExTreeViewItem();
}
}
public class ExTreeViewItem : System.Windows.Controls.TreeViewItem
{
public ExTreeViewItem()
{
SetBinding(IsExpandedProperty, new Binding("IsExpanded") { Mode = BindingMode.TwoWay });
}
protected override DependencyObject GetContainerForItemOverride()
{
return new ExTreeViewItem();
}
}
Then use ExTreeView instead of TreeView, it will automatically data bind to IsExpanded property of hierarchical data item.