没有集合的 TreeView 递归

发布于 2024-10-13 01:57:20 字数 4034 浏览 2 评论 0原文

如果您的数据按集合进行组织,则可以使用 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 对我来说不是一个选择。有什么想法,我做错了什么吗?

if your data is organized in collections, you can populate a TreeView by using HierarchicalDataTemplates. But i have some data, saved in few well known classes, that needs to be displayed in a TreeView. Here is a code example:

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());
    }
}

And here is the xaml-code:

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

The example works fine, but you can't select the items which are generated in the DataTemplate for GroupWith2Children. It seems that the TreeView treats the entire template as one item. You can only select the root node and the entire subtree "GroupWith2Children".

To replace GroupWith2Children with a collection is not an option for me. any ideas, what i'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2024-10-20 01:57:20

您不必将 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;
      }
   }
}

You don't have to replace GroupWith2Children with a collection; you can implement an enumerable property. And if you can't touch the GroupWith2Children class at all, implement a wrapper class:

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;
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文