wpf 使用 HierarchicalDataTemplate 显示混合类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12023830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:01:10  来源:igfitidea点击:

Displaying mixed types using HierarchicalDataTemplate

wpftreeviewhierarchicaldatatemplate

提问by 0wl

I am trying to display a tree view with mixed types using HierarchicalDataTemplate but so far all my attempts failed..

我正在尝试使用 HierarchicalDataTemplate 显示混合类型的树视图,但到目前为止我所有的尝试都失败了..

I have a tree-like class that can have two different types of children. (Location and Device). To make things understandable here is the illustration of what I am trying to display:

我有一个类似树的类,可以有两种不同类型的孩子。(位置和设备)。为了使事情易于理解,这里是我试图展示的内容的说明:

->Location 1
    |
    |--->Device 1.1
    |--->Device 1.2
    |--->Location 1.2
          |
          |---->Device 1.2.1 
          |---->Location 1.2.1
            .....etc.....

I have tried a lot of solutions I found but none worked. In the most cases I just get the class name in the tree view. Is what I am trying to do even possible using HierarchicalDataTemplate? If it is please let me know how.

我已经尝试了很多我找到的解决方案,但没有一个奏效。在大多数情况下,我只是在树视图中获取类名。我正在尝试使用 HierarchicalDataTemplate 做的事情甚至可能吗?如果是,请让我知道如何。

回答by 0wl

After several hours of trial and error I went back to the beginning and found the solution. I can't believe it is actually so simple.

经过几个小时的反复试验,我回到了起点并找到了解决方案。我不敢相信它实际上如此简单。

The template looks like this:

模板如下所示:

<HierarchicalDataTemplate x:Key="NavigatorDataTemplate" DataType="{x:Type local:Location}" ItemsSource="{Binding Locations}">
    <StackPanel>
        <TextBlock Text="{Binding Description}"/>
        <ListBox ItemsSource="{Binding Devices}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Description}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</HierarchicalDataTemplate>

The TreeView is just this:

TreeView 就是这样的:

<TreeView Name="treeNav" ItemTemplate="{StaticResource NavigatorDataTemplate}"/>

And since the collection that needs to be displayed is in the code already i just set it to the ItemsSource:

由于需要显示的集合已经在代码中,我只需将其设置为 ItemsSource:

treeNav.ItemsSource = locations;

回答by outcoldman

The HierarchicalDataTemplate should help to solve your problem. Take a look on latest sample on MSDN Data Templating Overview

HierarchicalDataTemplate 应该有助于解决您的问题。查看 MSDN数据模板概述上的最新示例

回答by Rohit Vats

For this hierarchy to work Locationobject should have composite collection of Locations and Devicesand you simply need to set that collection as ItemsSource of your HierarchicalDataTemplate. This sample should work then -

要使这个层次结构工作Location对象应该具有composite collection of Locations and Devices,您只需将该集合设置为您的 HierarchicalDataTemplate 的 ItemsSource。这个样本应该可以工作 -

<HierarchicalDataTemplate DataType="{x:Type local:Location}"
                          ItemsSource="{Binding CompositeCollection}">
   <TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>

<DataTemplate DataType="{x:Type local:Device}">
   <TextBlock Text="{Binding Name}"/>
</DataTemplate>

I am assuming you have Name property in each object Location and Device which you need to show on UI. Add these data templates to resources of your ItemsControl which may be ListBox, TreeView or any other ItemsControl.

我假设您在需要在 UI 上显示的每个对象 Location 和 Device 中都有 Name 属性。将这些数据模板添加到 ItemsControl 的资源中,这些资源可以是 ListBox、TreeView 或任何其他 ItemsControl。

回答by Gary Kindel

Being new to WPF, I recently solved this same problem. The approach I used was to create a new class that would be used to display the data in the tree.

作为 WPF 的新手,我最近解决了同样的问题。我使用的方法是创建一个新类,用于在树中显示数据。

TreeDisplayItem Class has contractor per class type that will be displayed. In each constructor, the _display property will be set for the class passed.

TreeDisplayItem 类具有将显示的每个类类型的承包商。在每个构造函数中,都会为传递的类设置 _display 属性。

itemDef = Category Name

itemDef = 类别名称

ItemPrice = Price + Quantity + Store location

ItemPrice = 价格 + 数量 + 店铺位置

    public class TreeDisplayItem
{
    readonly string _display;
    readonly string _id;
    readonly List<TreeDisplayItem> _items;

    public LegoPriceDisplay(ItemDef itemDef, List<TreeDisplayItem> items)
    {
      ...//Root node class type  ItemDef 
    }

    public LegoPriceDisplay(ItemPrice price)
    {
       ....//child node type = ItemPrice  
    }

    public List<TreeDisplayItem> Items{ get; private set; }


    public string DisplayId 
    { 
        get { return _id; } 
    }


    public string Display
    {
        get { return _display; }
    }


}

In the WPF Page XAML, define a treeview with nested HierarchicalDataTemplates. Of course this works for only use cases where you have fixed / Known tree item depth. In my case, treeview will drill down to two layers (counting the root)

在 WPF 页面 XAML 中,使用嵌套的 HierarchicalDataTemplates 定义树视图。当然,这仅适用于具有固定/已知树项深度的用例。在我的情况下,树视图将深入到两层(计算根)

 <TreeView Margin="0" Name="TvStorePrices" ItemsSource="{Binding TreeDisplayItemList}">
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="local:TreeDisplayItem" ItemsSource="{Binding Items}">
     <TextBlock Text="{Binding Display}" />
          <HierarchicalDataTemplate.ItemTemplate>
              <HierarchicalDataTemplate DataType="local:TreeDisplayItem" ItemsSource="{Binding Items}">
                    <TextBlock Text="{Binding Display}" />
              </HierarchicalDataTemplate>
         </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>