40

WPF Treeview Binding 的情况有点复杂。我花了过去 2 天的时间尝试谷歌它,是我想出的关闭,但它并没有解决问题。

情况如下:

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

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

每个类别可以有一个对象列表和子类别。我有这样做的理由,这对我和我正在编写的应用程序来说完全有意义。

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

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 看起来与上述对象构造几乎相同。

到目前为止,我的 XAML Treeview 看起来像这样:

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

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

有没有办法直接使用模板来执行此操作,以便选择每个项目(类别或产品)?我正在使用 MVVM 模式,不想求助于使用后面的代码,但如果有必要的话。

4

1 回答 1

61

由于您希望 TreeView 中的元素具有包含两个类别产品的子项列表,因此您将希望类别视图模型具有一个包含类别和产品的集合。例如,您可以使用CompositeCollection来组合现有的集合:

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

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

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

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

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

于 2010-09-09T01:45:58.783 回答