2009-11-11 259 views
15

我只是试图找到一种方法来控制TreeView节点通过它们绑定的对象的展开/折叠。该对象有一个IsExpanded属性,我想用它来显示TreeView节点本身根据该属性展开或折叠。WPF DataBound treeview展开/折叠

这里是我的代码:

C#:

public partial class Window2 : Window 
{ 
    public Window2() 
    { 
     InitializeComponent(); 

     this.DataContext = new List<Parent>() { Base.GetParent("Parent 1"), Base.GetParent("Parent 2") }; 
    } 
} 

public class Base 
{ 
    public string Name { get; set; } 
    public bool IsExpanded { get; set; } 

    public static Parent GetParent(string name) 
    { 
     Parent p = new Parent() { Name = name }; 

     p.Children.Add(new Child() { Name = "Child 1", GrandChildren = new ObservableCollection<GrandChild>() { new GrandChild() { Name = "Grandchild 1" } } }); 
     p.Children.Add(new Child() { Name = "Child 2", GrandChildren = new ObservableCollection<GrandChild>() { new GrandChild() { Name = "Grandchild 1" } } }); 
     p.Children.Add(new Child() { Name = "Child 3", GrandChildren = new ObservableCollection<GrandChild>() { new GrandChild() { Name = "Grandchild 1" } } }); 

     return p; 
    } 
} 

public class Parent : Base 
{ 
    public ObservableCollection<Child> Children { get; set; } 

    public Parent() 
    { 
     this.Children = new ObservableCollection<Child>(); 
    } 
} 

public class Child : Base 
{ 
    public ObservableCollection<GrandChild> GrandChildren { get; set; } 

    public Child() 
    { 
     this.GrandChildren = new ObservableCollection<GrandChild>(); 
    } 
} 

public class GrandChild : Base 
{ 
} 

XAML:

<Window x:Class="HeterogeneousExperimentExplorer.Window2" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:HeterogeneousTree" 
    Title="Window2" Height="300" Width="300"> 
    <Window.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type local:Parent}" ItemsSource="{Binding Children}"> 
      <TextBlock Text="{Binding Name}" /> 
      <HierarchicalDataTemplate.ItemTemplate> 
       <HierarchicalDataTemplate DataType="{x:Type local:Parent}" ItemsSource="{Binding GrandChildren}"> 
        <TextBlock Text="{Binding Name}" /> 
        <HierarchicalDataTemplate.ItemTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding Name}" /> 
         </DataTemplate> 
        </HierarchicalDataTemplate.ItemTemplate> 
       </HierarchicalDataTemplate> 
      </HierarchicalDataTemplate.ItemTemplate> 
     </HierarchicalDataTemplate> 
    </Window.Resources> 
    <Grid> 
     <TreeView ItemsSource="{Binding}" /> 
    </Grid> 
</Window> 

回答

40

想出了解决方案。很简单:

<Style TargetType="{x:Type TreeViewItem}"> 
     <Setter Property="IsExpanded" Value="{Binding IsNodeExpanded}"> 
     </Setter> 
    </Style> 

所以Style获取绑定到树型视图对象并查看它的属性IsNodeExpanded,它将该值分配到TreeViewItem.IsExpanded财产。如果添加Mode = TwoWay,他们会通知对方(TreeViewItem会在对象被展开时告诉对象)。

谢谢!

1

FWIW,您可能对此CodeProject article by Josh Smith感兴趣,它展示了如何使用通用(n级)方法创建基于MVVM的树视图。

我并不是说Carlo的实现有什么问题,但我发现这篇文章对理解TreeView控件和MVVM有帮助。