我可以在TreeView层次结构的同一级别拥有不同的类吗?

时间:2013-02-13 20:17:04

标签: c# wpf treeview

我有一个类的层次结构,我想在WPF TreeView中显示。

  • 任务
    • 产品

这些类包括字符串属性和集合属性。它看起来像这样:

  • 洗衣

    • 约翰
    • 物品收藏
      • 衣服
      • 洗衣粉
    • Days Collection
      • 星期日
      • 星期四
  • 购物

    • 米利
    • 产品
      • 货币
      • 列表
      • 汽车
      • 星期六

我想将字符串(Person)显示为叶子,并且能够深入到集合(Items,Days)以到达内部的字符串。

我发现此解释http://social.msdn.microsoft.com/forums/en-US/wpf/thread/e40e0a8f-7758-4b69-80f6-1c657294d019/适用于此布局:

  • 任务
      • 产品

但是我无法弄清楚如何调整它来代表同一层级的不同类。所有的帮助,尤其是一个例子。

1 个答案:

答案 0 :(得分:2)

我认为,我已经解决了这个问题。让我知道是否有比这更明智的方法。

这篇博文有很多帮助,但遗憾的是代码中充斥着错别字:

David Sackstein's - HierarchicalDataTemplate and TreeView

关键是绑定到TreeView的每个类都是从基类派生的。这允许您通过将不同类型绑定到其基类的集合来将不同类型添加到TreeView中。然后在XAML中,您可以为每个DataType创建一个HierarchicalDataTemplate,一切正常。

enter image description here

<Window x:Class="HierarchicalDataTemplateAndTreeView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:HierarchicalDataTemplateAndTreeView"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <HierarchicalDataTemplate DataType="{x:Type local:Task}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal" Margin="3">
                <TextBlock Text="{Binding Path=Name}" FontSize="16" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:Person}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" Foreground="Purple" FontSize="14" FontWeight="Bold" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:ItemCollection}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" Foreground="Blue" FontSize="12" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:Item}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" Foreground="Blue" FontSize="11" FontStyle="Italic" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:DayCollection}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" Foreground="RosyBrown" FontSize="12" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:Day}" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" Foreground="RosyBrown" FontSize="12" FontStyle="Italic" />
            </StackPanel>
        </HierarchicalDataTemplate>

    </Window.Resources>
    <StackPanel>
        <TreeView Name="treeView"/>
    </StackPanel>
</Window>

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace HierarchicalDataTemplateAndTreeView
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            treeView.ItemsSource = GetData();
        }

        public List<Composite> GetData()
        {
            List<Composite> list = new List<Composite>()
            {
                new Task
                { 
                    Name = "Laundry", Children = new List<Composite>()
                    {
                        new Person { Name = "John" },
                        new ItemCollection
                        { 
                            Name = "Items", Children = new List<Composite>()
                            {
                                new Item { Name = "Clothes" },
                                new Item { Name = "Washing Powder" }
                            }
                        },

                        new DayCollection
                        {
                            Name = "Days", Children = new List<Composite>()
                            {
                                new Day { Name = "Sunday" },
                                new Day { Name = "Thursday" }
                            }
                        }
                    }
                },

                new Task
                { 
                    Name = "Shopping", Children = new List<Composite>()
                    {
                        new Person { Name = "Millie" },
                        new ItemCollection
                        { 
                            Name = "Items", Children = new List<Composite>()
                            {
                                new Item { Name = "Money" },
                                new Item { Name = "List" },
                                new Item { Name = "Bags" }
                            }
                        },

                        new DayCollection
                        {
                            Name = "Days", Children = new List<Composite>()
                            {
                                new Day { Name = "Saturday" }
                            }
                        }
                    }
                }
            };

            return list;
        }
    }
}

using System.Collections.Generic;

namespace HierarchicalDataTemplateAndTreeView
{
    public class Composite
    {
        public string Name { get; set; }
        public List<Composite> Children { get; set; }
    }

    public class Task: Composite
    {
    }

    public class Person : Composite
    {
    }

    public class ItemCollection : Composite
    {
    }

    public class Item : Composite
    {
    }

    public class DayCollection : Composite
    {
    }

    public class Day : Composite
    {
    }
}