WPF DataBound树视图展开/折叠

Car*_*rlo 17 data-binding wpf treeview expand collapse

我只是想找到一种方法来控制TreeView节点通过它们绑定的对象的扩展/折叠.该对象具有一个IsExpanded属性,我想使用它来显示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
{
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Car*_*rlo 43

想出解决方案.真的很简单:

    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsNodeExpanded}">
        </Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

因此,样式获取绑定到TreeViewItem的对象并查看其IsNodeExpanded属性,并将该值分配给TreeViewItem.IsExpanded属性.如果添加Mode = TwoWay,它们将相互通知(TreeViewItem将在对象扩展时告诉它)​​.

谢谢!

  • 并记住将`Mode=TwoWays` 设置为完整功能 (3认同)
  • @NirMH 它应该在`TreeView.Resources` (2认同)
  • 也有用的链接/sf/ask/104100111/ (2认同)