与混合型WPF树视图中的数据绑定的层级数据 [英] WPF Treeview Databinding Hierarchal Data with mixed types

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

问题描述

我有一点与WPF树视图绑定一个复杂的局面。我花了近2天尝试谷歌它,<一个href=\"http://stackoverflow.com/questions/1214967/how-to-use-the-wpf-treeview-with-nested-objects-of-different-types\"标题=类似的问题>这是封闭我想到了,但它并没有解决这个问题。

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。我想在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.

有没有什么办法,使每个项目(类别或产品)选择与模板直接做到这一点?我使用的是MVV​​M模式,不希望诉诸使用code背后,却会如果它是必要的。

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的元素有一个包含这两类产品的儿童的名单,你会希望你的分类视图模型有一个包含一个集合两个类别和产品。例如,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.compositecollection.aspx\">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 }
            };
        }
    }
}

(在现实code,你可能会想保持相同的集合对象的引用,而不是每次都创建一个新的。)

(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}">

该项目将产品和类别对象的组合,WPF将使用适当的DataTemplate中为每一个。

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

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

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