c# - 没有集合的 TreeView 递归

标签 c# wpf templates treeview

如果您的数据组织在集合中,您可以使用 HierarchicalDataTemplates 填充 TreeView。但是我有一些数据,保存在几个众所周知的类中,需要在 TreeView 中显示。这是一个代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Test
{
    public class GroupWithList
    {
        public string Name { get; set; }

        public ObservableCollection<string> Items { get; set; }

        public GroupWithList(string name)
        {
            this.Name = name;
            this.Items = new ObservableCollection<string>();
        }
    }

    public class GroupWith2Children
    {
        public string Name { get; set; }

        public ObservableCollection<GroupWithList> Groups { get; set; }

        public GroupWithList First
        {
            get
            {
                return this.Groups[0];
            }
        }

        public GroupWithList Second
        {
            get
            {
                return this.Groups[1];
            }
        }

        public GroupWith2Children()
        {
            this.Name = "GroupWith2Children";
            this.Groups = new ObservableCollection<GroupWithList>();

            GroupWithList g;

            g = new GroupWithList("Letters");
            g.Items.Add("u");
            g.Items.Add("a");
            g.Items.Add("t");
            this.Groups.Add(g);

            g = new GroupWithList("Numbers");
            g.Items.Add("12");
            g.Items.Add("153");
            this.Groups.Add(g);
        }
    }
}

public partial class MainWindow : Window
{
    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<GroupWith2Children>), typeof(MainWindow), new FrameworkPropertyMetadata(new ObservableCollection<GroupWith2Children>()));

    public ObservableCollection<GroupWith2Children> Items
    {
        get
        {
            return (ObservableCollection<GroupWith2Children>)this.GetValue(ItemsProperty);
        }

        set
        {
            this.SetValue(ItemsProperty, value);
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        this.Items.Add(new GroupWith2Children());
        this.Items.Add(new GroupWith2Children());
    }
}

这是 xaml 代码:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:test="clr-namespace:Test"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>

            <DataTemplate DataType="{x:Type test:GroupWith2Children}">
                <TreeViewItem Header="{Binding Mode=OneWay, Path=Name}">
                    <TreeViewItem Header="First" ItemsSource="{Binding Mode=OneWay, Path=First.Items}"/>
                    <TreeViewItem Header="Second" ItemsSource="{Binding Mode=OneWay, Path=Second.Items}"/>
                </TreeViewItem>
            </DataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type test:GroupWithList}" ItemsSource="{Binding Path=Items}">
                <TextBlock Text="GroupWithList"/>
            </HierarchicalDataTemplate>

            <DataTemplate DataType="{x:Type sys:String}">
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </Grid.Resources>

        <TreeView>
            <TreeViewItem Header="Root" ItemsSource="{Binding Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Items}"/>
        </TreeView>
    </Grid>
</Window>

该示例运行良好,但您无法选择在 GroupWith2Children 的 DataTemplate 中生成的项目。 TreeView 似乎将整个模板视为一个项目。您只能选择根节点和整个子树“GroupWith2Children”。

用集合替换 GroupWith2Children 对我来说不是一个选项。任何想法,我做错了什么?

最佳答案

您不必用集合替换 GroupWith2Children;你可以实现一个可枚举的属性。如果您根本无法触及 GroupWith2Children 类,请实现一个包装类:

public class GroupWrapper()
{
   public GroupWrapper(GroupWith2Children group)
   {
      Group = group;
   }

   public GroupWith2Children Group { get; private set; ]

   public IEnumerable<GroupWithList> Groups
   {
      get
      {
         yield return Group.First;
         yield return Group.Second;
      }
   }
}

关于c# - 没有集合的 TreeView 递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4751242/

相关文章:

c++ - 使用 Armadillo 库进行意外(错误)的模板推导

c# - 如何在 TabControl 中设置新 TabPage 页面的属性?

c# - 如何在不拉起任务栏的情况下启动新程序?

c# - 如何在不同线程上创建用户控件?

c# - 更改按钮内的椭圆填充颜色

c++ - 在模板派生类中,为什么需要在成员函数中使用“this->”来限定基类成员的名字?

c# - 如何保证在双屏场景下 "additional"屏显示一个窗体?

c# - 数据表填充对象

WPF 绑定(bind)到自身

c++ - 返回推力二值函数