views:

32

answers:

1

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

A: 

I'm not sure if this works in Silverlight or not, but in WPF you can bind to IsExpanded by using a style:

<sdk:TreeView ItemsSource="{Binding RootNodes}">
    <sdk:TreeView.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
        </Style>
    </sdk:TreeView.Resources>
    <sdk:TreeView.ItemTemplate>
        <sdk:HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
            <TextBlock Text="{Binding Name}" />
        </sdk:HierarchicalDataTemplate>
    </sdk:TreeView.ItemTemplate>
</sdk:TreeView>
Andy
That was also my first guess :)But no, it does not work in Silverlight.
LexL