WPF DataBound树视图展开/折叠

编程入门 行业动态 更新时间:2024-10-07 10:19:49
本文介绍了WPF DataBound树视图展开/折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是试图通过他们绑定的对象找到一种控制 TreeView 节点的扩展/崩溃的方法。该对象有一个 IsExpanded 属性,我想使用它来显示基于...的 TreeView 节点本身展开或折叠这个财产。

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.

这是我的代码:

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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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>

推荐答案

解决方案。真的很简单:

Came up with solution. Really simple:

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

因此,样式将对象绑定到TreeViewItem,并查看其IsNodeExpanded属性,并分配该值到TreeViewItem.IsExpanded属性。如果你添加了Mode = TwoWay,他们将通知对方(TreeViewItem会在对象扩展时告诉对象)。

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).

谢谢!

更多推荐

WPF DataBound树视图展开/折叠

本文发布于:2023-11-12 20:49:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   WPF   DataBound

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!