【问题标题】:Displaying mixed types using HierarchicalDataTemplate使用 HierarchicalDataTemplate 显示混合类型
【发布时间】:2012-08-19 02:48:44
【问题描述】:

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

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

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

我已经尝试了很多我找到的解决方案,但都没有奏效。在大多数情况下,我只是在树视图中获取类名。 使用 HierarchicalDataTemplate 甚至可以尝试做的事情吗?如果是,请告诉我怎么做。

【问题讨论】:

  • 您能否发布您对 HierarchicalDataTemplate 的示例用法?此外,使用 Snoop(一种可让您调试实时 WPF/Silverlight 应用程序的工具)可以帮助您查明绑定失败的原因。
  • 感谢有关 Snoop 的提示!我需要这样的工具。

标签: wpf treeview hierarchicaldatatemplate


【解决方案1】:

经过几个小时的反复试验,我回到了起点并找到了解决方案。我不敢相信它居然这么简单。

模板如下所示:

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

TreeView 就是这样:

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

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

treeNav.ItemsSource = locations;

【讨论】:

  • 不错的小例子。为了完整起见,如果你也可以,但这里的类的代码(我认为只是几行)会很有用。
【解决方案2】:

HierarchicalDataTemplate 应该有助于解决您的问题。查看 MSDN 上的最新示例 Data Templating Overview

【讨论】:

  • 在看到你的回答之前我实际上已经解决了,但是谢谢,这很有帮助。
【解决方案3】:

为了使这个层次结构工作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>

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

【讨论】:

    【解决方案4】:

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

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

    itemDef = 类别名称

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

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

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

    【讨论】:

    • 既然你可以显示混合类型(见答案),创建额外的类有点过头了。
    • 我同意,但我很难正确配置混合类型模板。这是我发现有效的初始解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2015-11-04
    • 2014-01-10
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    相关资源
    最近更新 更多