WPF树数据模板(取决于项目类型) [英] WPF Tree Datatemplate Depending on Type of Item

查看:102
本文介绍了WPF树数据模板(取决于项目类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVVM WPF应用程序,其中包含一个包含自引用数据的树,该数据通过分层转换器绑定到该树. (例如: http: //www.telerik.com/help/wpf/radtreeview-how-to-bind-to-self-referencing-data.html )

I have a MVVM WPF application with a tree containing self referenced data, this data is bound to the tree with a hierarchical converter. (As in example: http://www.telerik.com/help/wpf/radtreeview-how-to-bind-to-self-referencing-data.html)

此数据包括ID,ParentID,Text和Type(以及更多).

This data includes ID, ParentID, Text and Type (and more).

示例数据:

1,0,"FirstItem","Triangle"
2,1,"SubItem1","Circle"
3,1,"SubItem2","Square"
4,2,"SubItem11","Triangle"
5,2,"SubItem12","Heart"
6,3,"SubItem21","Circle"

现在,我想为三角形",心形",圆形"和正方形"设置不同的模板. 我的意思不是只更改图像,而是更改模板. 我该怎么做?

Now I would like to have different templates for the Triangles, Hearts, Circles and Squares. I dont mean only change a Image, but really a Template. How can i accomplish such?

亲切的问候,保罗.

推荐答案

如果您的ItemsSource由不同的Types组成,则只需创建HierarchicalDataTemplates而不分配x:Key.如果DataTemplate没有x:Key属性,则框架会在遇到类型时尝试使用此DataTemplate并尝试以视觉方式显示它(您可以阅读有关隐式DataTemplates

If your ItemsSource is made up of different Types, then you can simply create HierarchicalDataTemplates and not assign an x:Key. If there is no x:Key attribute for a DataTemplate, the framework will use this DataTemplate when it comes across the type and tries to visually display it (you can read more about implicit DataTemplates here). For example, if you have a type Circle and another type Square, you would have the following templates in your Resources:

<Window.Resources>
    <HierarchicalDataTemplate DataType="{x:Type local:Circle}" ItemsSource="{Binding Children}">
        <Ellipse Fill="{Binding Fill}" Width="25" Height="25" Stroke="Black" StrokeThickness="0.25"/>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:Square}" ItemsSource="{Binding Children}">
        <Rectangle Fill="{Binding Fill}" Width="25" Height="25" Stroke="Black" StrokeThickness="0.25"/>
    </HierarchicalDataTemplate>
</Window.Resources>

然后,如果您的TreeView在其ItemsSource中遇到这些类型之一,它将为该特定类型使用HierarchicalDataTemplate.

Then if your TreeView encounters one of these Types in its ItemsSource, it will use the HierarchicalDataTemplate for that specific type.

您可以在HierarchicalDataTemplates 此处了解更多信息>和此链接举例说明了如何在TreeView中使用它们.

You can read more about HierarchicalDataTemplates here, and this link has an example of how they are used in a TreeView.

OR

如果您的商品都是相同的Type,并且仅按属性(例如Type)进行区分,则需要使用DataTemplateSelector.这是一个简单的例子:

If your items are all the same Type, and only differentiated by a property (such as Type), you'll need to use a DataTemplateSelector. Here is a simple example of one:

隐藏代码:

public class ShapeTemplateSelector : DataTemplateSelector
{
    public DataTemplate CircleTemplate { get; set; }
    public DataTemplate SquareTemplate { get; set; }
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        Shape shape = item as Shape;
        if (shape != null)
        {
            if (shape.Type == "Circle")
                return this.CircleTemplate;
            else if (shape.Type == "Square")
                return this.SquareTemplate;
            }
            return null;
        }
}

和XAML:

<local:ShapeTemplateSelector x:Key="shapeSelector">
    <local:ShapeTemplateSelector.CircleTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:Shape}" ItemsSource="{Binding Children}">
            <Ellipse Fill="{Binding Fill}" Width="25" Height="25" Stroke="Black" StrokeThickness="0.25"/>
        </HierarchicalDataTemplate>
    </local:ShapeTemplateSelector.CircleTemplate>
    <local:ShapeTemplateSelector.SquareTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:Shape}" ItemsSource="{Binding Children}">
            <Rectangle Fill="{Binding Fill}" Width="25" Height="25" Stroke="Black" StrokeThickness="0.25"/>
        </HierarchicalDataTemplate>
    </local:ShapeTemplateSelector.SquareTemplate>
</local:ShapeTemplateSelector>

然后在TreeView中,只需分配选择器

Then in your TreeView, you simply assign the selector

<TreeView x:Name="Tree" ItemsSource="{Binding Shapes}" ItemTemplateSelector="{DynamicResource shapeSelector}"/>

这篇关于WPF树数据模板(取决于项目类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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