2011-09-27 44 views
3

我在Silverlight 4中使用分层树视图。根据用户的操作,可以清除并重建此树。当发生这种情况时,默认情况下树会折叠,这可能会从用户的角度来看令人讨厌。Silverlight树视图:保存展开/折叠状态

所以,我想以某种方式保存哪些节点被展开,这样我可以在清除并重新加载后恢复树的视觉状态。

我的TreeView的是这样实现的:

xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
xmlns:controls2="clr-namespace:System.Windows;assembly=System.Windows.Controls" 

<controls:TreeView x:Name="Tree" 
    ItemsSource="{Binding Source={StaticResource ViewModel}, Path=TreeStructure, Mode=OneWay}" 
    ItemTemplate="{StaticResource hierarchicalTemplate}" /> 

<controls2:HierarchicalDataTemplate x:Key="hierarchicalTemplate" ItemsSource="{Binding Children}"> 
     <TextBlock Text="{Binding Value.DisplayName}"> 
</controls2:HierarchicalDataTemplate> 

我的TreeView的ItemsSource绑定在一个ObservableCollection TreeStructure;

节点是一个包装类,看起来像这样:

public class Node 
{ 
    public object Value { get; private set; } 

    public ObservableCollection<Node> Children { get; private set; } 

    public Node(object value) 
    { 
     Value = value; 
     Children = new ObservableCollection<Node>(); 
    } 
} 

漂亮的标准的东西。我看到了WPF的一些解决方案,但我可以找到任何东西的Silverlight树形视图...

有什么建议吗?

谢谢!

回答

3

鉴于您将数据实现为树的方式,为什么不将“TreeViewItem.IsExpanded”依赖项属性绑定到您自己的Node上的bool属性?

它至少需要一个INotifyPropertyChanged属性,因此节点需要实现INotifyPropertyChanged

在Silverlight 5,你可以只设置一个样式像这样绑定到IsExpanded属性:

<Style TargetType="sdk:TreeViewItem" x:Key="itemStyle"> 
    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
</Style> 

,并且使用带有

ItemContainerStyle="{Binding Source={StaticResource itemStyle}}" 

在Silverlight 4中也有一些变通办法。

+0

我喜欢这个建议!也许这是一个基本的问题,但我如何绑定TreeViewItem.IsExpanded?似乎我无法直接从我的XAML访问该属性。 – Mathieu

+0

@Tigel:我一直在设置一个IsExpanded属性绑定的例子,但是你的特定虚拟机和安装程序让我留下了一个空白树。你能提供更多的设置代码或完整的工作示例吗? –

+0

是的,我得到它的工作,我会在几个解决方案。感谢您指点我正确的方向。 Silverlight 5解决方案更加优雅!太糟糕我正在使用SL4 :( – Mathieu

1

下面是我在TreeViewItem.IsExpanded属性上绑定的操作。首先,我在Node类中添加了一个IsExpanded属性。

public class Node : INotifyPropertyChanged 
{ 
    public object Value { get; private set; } 

    public ObservableCollection<Node> Children { get; private set; } 

    private bool isExpanded; 
    public bool IsExpanded 
    { 
     get 
     { 
      return this.isExpanded; 
     } 
     set 
     { 
      if (this.isExpanded != value) 
      { 
       this.isExpanded = value; 
       NotifyPropertyChanged("IsExpanded"); 
      } 
     } 
    } 

    public Node(object value) 
    { 
     Value = value; 
     Children = new ObservableCollection<Node>(); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

在那之后,我子类TreeView和树型视图控件(我失去了自定义主题在我的TreeView的,但无论...)

public class BindableTreeView : TreeView 
{ 
    protected override DependencyObject GetContainerForItemOverride() 
    { 
     var itm = new BindableTreeViewItem(); 
     itm.SetBinding(TreeViewItem.IsExpandedProperty, new Binding("IsExpanded") { Mode = BindingMode.TwoWay }); 

     return itm; 
    } 
} 

public class BindableTreeViewItem : TreeViewItem 
{ 
    protected override DependencyObject GetContainerForItemOverride() 
    { 
     var itm = new BindableTreeViewItem(); 
     itm.SetBinding(TreeViewItem.IsExpandedProperty, new Binding("IsExpanded") { Mode = BindingMode.TwoWay }); 

     return itm; 
    } 
} 

在我的XAML中,我只需要使用BindableTreeView而不是TreeView,它的工作原理。

0

诀窍是使用SetterValueBindingHelperhere。那么你的XAML将如下所示。请确保您仔细复制下面的内容。

<sdk:TreeView.ItemContainerStyle> 
    <Style TargetType="sdk:TreeViewItem"> 
     <Setter Property="local:SetterValueBindingHelper.PropertyBinding"> 
      <Setter.Value> 
       <local:SetterValueBindingHelper> 
        <local:SetterValueBindingHelper Property="IsSelected" Binding="{Binding Mode=TwoWay, Path=IsSelected}"/> 
        <local:SetterValueBindingHelper Property="IsExpanded" Binding="{Binding Mode=TwoWay, Path=IsExpanded}"/> 
       </local:SetterValueBindingHelper> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</sdk:TreeView.ItemContainerStyle> 

语法与您在WPF中使用的不完全相同,但它的工作原理和工作原理都很好!