3

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

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

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

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

4

4 回答 4

5

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

模板如下所示:

<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;
于 2012-08-19T14:14:40.947 回答
1

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

于 2012-08-19T03:26:09.170 回答
1

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

于 2012-08-19T07:31:49.673 回答
0

作为 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 中,使用嵌套的 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>

于 2013-10-19T15:45:47.720 回答