2009-11-11 147 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有幫助。