WPF 中的条件列表项模板或数据模板 [英] Conditional List itemtemplate or datatemplate in WPF

查看:34
本文介绍了WPF 中的条件列表项模板或数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个显而易见的问题,但我认为可能有多种实现方式,所以这不仅对我有用,而且希望对其他人有用.

本质上,我正在寻找实现列表视图的最佳方法,该视图可以接受不同类型的对象,然后使用该对象的适当项目/数据模板呈现它们.

例如……我们有一个标准的产品列表视图,当我们查看不同的类别时,企业决定为每个不同的类别显示不同的项目模板样式.

在这里提出这个问题的主要原因是为了避免讨厌的hacky解决方案,而是发现一个好的清洁方法.

希望我提供了足够的信息,如果您需要更多信息,请告诉我.

解决方案

只需在 Resources 中指定 DataTemplates 与相应的 也可以用于隐式 XML 数据模板(参见文档),因此属性类型是 not System.Type,所以与 Style.TargetType 不同,你必须使用 x:Type 引用一个 CLR 类型.如果你只是输入一个字符串,它不会被转换成一个类型.)

您可能还想查看 CompositeCollections,以获得不同类型的干净合并列表.

<小时>

我使用的示例数据:

ObservableCollection数据 = new ObservableCollection(new Employee[]{new Employee("Hans", "Programmer") ,new Employee("Elister", "Programmer") ,new Employee("Steve", "GUI Designer") ,new Employee("Stephen", "GUI Designer") ,new Employee("Joe", "Coffee Getter") ,new Employee("Julien", "Programmer") ,new Employee("John", "Coffee Getter") ,});ObservableCollection<Machine>data2 = new ObservableCollection(new Machine[]{new Machine("XI2", String.Empty),new Machine("MK2-xx", String.Empty),new Machine("A2-B16", String.Empty),});CompositeCollection cc1 = new CompositeCollection();cc1.Add(new CollectionContainer() { Collection = data });cc1.Add(new CollectionContainer() { Collection = data2 });数据 = cc1;

This may be an obvious question, but I think there may well be multiple ways to implement it, so not only will this be useful to me, hopefully it will be useful to others.

Essentially I'm looking for the best way to implement a list view that can accept different types of objects and then renders them with the appropriate item/data template for that object.

So for example... we have a standard product list view, and when we view different categories the business has decided it would like to show a different item template style for each different category.

The main reason for asking this here, is to avoid a nasty hacky solution and discover a good clean method instead.

Hopefully I've provided enough information, let me know if you need more.

解决方案

Just specifying DataTemplates in the Resources with the respective DataType is enough, e.g.

<ListView ItemsSource="{Binding Data}">
    <ListView.Resources>
        <!-- Do NOT set the x:Key -->
        <DataTemplate DataType="{x:Type local:Employee}">
            <TextBlock Text="{Binding Name}" Foreground="Blue"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Machine}">
            <TextBlock Text="{Binding Model}" Foreground="Red"/>
        </DataTemplate>
    </ListView.Resources>
</ListView>

(Note that DataTemplate.DataType can also be used for implicit XML data templating (see docs), the property type for that reason is not System.Type, so unlike in Style.TargetType you have to use x:Type to reference a CLR-type. If you just enter a string it will not be converted to a type.)

You might also want to look into CompositeCollections, to get clean merged lists of varying types.


Sample data i used:

ObservableCollection<Employee> data = new ObservableCollection<Employee>(new Employee[]
{
    new Employee("Hans", "Programmer")      ,
    new Employee("Elister", "Programmer")   ,
    new Employee("Steve", "GUI Designer")   ,
    new Employee("Stephen", "GUI Designer") ,
    new Employee("Joe", "Coffee Getter")    ,
    new Employee("Julien", "Programmer")    ,
    new Employee("John", "Coffee Getter")   ,
});
ObservableCollection<Machine> data2 = new ObservableCollection<Machine>(new Machine[]
{
    new Machine("XI2",    String.Empty),
    new Machine("MK2-xx", String.Empty),
    new Machine("A2-B16", String.Empty),
});
CompositeCollection cc1 = new CompositeCollection();
cc1.Add(new CollectionContainer() { Collection = data });
cc1.Add(new CollectionContainer() { Collection = data2 });
Data = cc1;

这篇关于WPF 中的条件列表项模板或数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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