混合类型的 WPF Treeview 数据绑定层次数据 [英] WPF Treeview Databinding Hierarchal Data with mixed types

查看:31
本文介绍了混合类型的 WPF Treeview 数据绑定层次数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 WPF 树视图绑定有一些复杂的情况.过去 2 天我一直在尝试谷歌它,并且 this 是我想出的封闭式,但它并没有解决问题.

I have a bit of a complex situation with WPF Treeview Binding. I have spent the last 2 days trying Google it, and this is the closed I came up with, but it doesn't solve the issue.

情况如下:

我有一个看起来像这样的对象:

I have an object that looks like this:

public class Category
{
  public string Name { get; set; }
  public List<Category> Categories { get; set; }
  public List<Product> Products { get; set; }
}

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

每个类别可以有一个对象列表和子类别.我有一个对我和我正在编写的应用程序完全有意义的理由.

Each Category can have a list of objects, and child categories. I have a reason for doing this that makes complete sense to me, and the app I am writing.

实际的对象构造可能看起来像这样:

The actual object construction may look Something like this:

Category - Pharmacy
  |-Product - Aspirin
  |-Product - Tylenol
  |-Category - Tooth Paste
  |  |-Product - Crest
  |  |-Product - Colgate
  |-Category - Paper Products
   |-Category - Toilet Paper
   |  |-Product - NoName
   |  |-Product - Charmin
   |-Category - Facial Tissue
      |-Product - Kleenex
Category - Household
  |-Product - Pinesol Cleaner
  |-Product - Garbage Bags

现在,我正在尝试将此关系数据绑定到树视图.我希望 TreeView 看起来与上述对象构造几乎相同.

Now, I am trying to databind this relationship to a treeview. I would like the TreeView to look almost identical to the above object construct.

到目前为止,我的 XAML 树视图如下所示:

So far I have my XAML Treeview look like this:

  <TreeView x:Name="CategoryList" Margin="8" Grid.Row="2" Grid.RowSpan="2" ItemsSource="{Binding Path=Categories}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type src:Category}" ItemsSource="{Binding Products}">
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type src:Product}">
                    <StackPanel>
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>

这非常适用于类别的主列表及其每个子产品.但它并没有更深入地显示每个类别下的子类别.

This works great for the main list of Categories, and each of it's sub products. But it doesn't go deeper and display the sub categories under each category.

有没有办法直接使用模板来做到这一点,以便选择每个项目(类别或产品)?我正在使用 MVVM 模式,不想诉诸于使用背后的代码,但如果有必要,我会这样做.

Is there any way to do this directly with Templates so that each item (category or product) is selected? I am using an MVVM pattern and don't want to resort to using the code behind, but will if it is necessary.

推荐答案

由于您希望 TreeView 中的元素具有包含两个类别产品的子项列表,因此您希望类别 ViewModel 具有包含以下内容的集合类别和产品.例如,您可以使用 CompositeCollection结合您现有的收藏:

Since you want the elements in the TreeView to have a list of children that consists of both Categories Products, you will want your Category ViewModel to have a collection that consists of both Categories and Products. For example, you could use a CompositeCollection to combine your existing collections:

public class Category
{
    public string Name { get; set; }
    public List<Category> Categories { get; set; }
    public List<Product> Products { get; set; }

    public IList Children
    {
        get
        {
            return new CompositeCollection()
            {
                new CollectionContainer() { Collection = Products },
                new CollectionContainer() { Collection = Categories }
            };
        }
    }
}

(在实际代码中,您可能希望保留对同一个集合对象的引用,而不是每次都创建一个新对象.)

(In real code, you would probably want to keep a reference to the same collection object rather than creating a new one each time.)

然后在您的 HierarchicalDataTemplate 中,使用组合列表作为 ItemsSource:

Then in your HierarchicalDataTemplate, use the combined list as the ItemsSource:

<HierarchicalDataTemplate DataType="{x:Type src:Category}"
                          ItemsSource="{Binding Children}">

这些项目将是 Product 和 Category 对象的混合体,WPF 将为每个对象使用适当的 DataTemplate.

The items will be a mix of Product and Category objects, and WPF will use the appropriate DataTemplate for each one.

这篇关于混合类型的 WPF Treeview 数据绑定层次数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆