WPF 数据绑定树视图展开/折叠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1717654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataBound treeview expand / collapse
提问by Carlo
I'm just trying to find a way to control the expand / collapse of the TreeView
nodes through the object they're bound to. The object has an IsExpanded
property, and I want to use that to show the TreeView
node itself expanded or collapsed based on that property.
我只是想找到一种方法来TreeView
通过节点绑定到的对象来控制节点的展开/折叠。该对象有一个IsExpanded
属性,我想用它来显示TreeView
基于该属性展开或折叠的节点本身。
Here's my code:
这是我的代码:
C#:
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:
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>
回答by Carlo
Came up with solution. Really simple:
想出了解决办法。真的很简单:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsNodeExpanded}">
</Setter>
</Style>
So the style gets the object bound to the TreeViewItem and looks at its IsNodeExpanded attribute and it assigns that value to the TreeViewItem.IsExpanded property. If you add Mode=TwoWay, they'll notify each other (TreeViewItem will tell the object when it has been expanded).
因此,样式获取绑定到 TreeViewItem 的对象并查看其 IsNodeExpanded 属性,并将该值分配给 TreeViewItem.IsExpanded 属性。如果添加 Mode=TwoWay,它们会相互通知(TreeViewItem 会在对象展开时通知对象)。
Thanks!
谢谢!
回答by si618
FWIW, you may be interested in this CodeProject article by Josh Smithwhich shows how to create a MVVM based tree view using a generic (n-level) approach.
FWIW,您可能对Josh Smith 的这篇CodeProject 文章感兴趣,该文章展示了如何使用通用(n 级)方法创建基于 MVVM 的树视图。
I'm not suggesting there is anything wrong with Carlo's implementation, but I found that article helpful in understanding the TreeView control, and MVVM in general.
我并不是说 Carlo 的实现有什么问题,但我发现那篇文章有助于理解 TreeView 控件和一般的 MVVM。