DataType的DataTemplate-如何在特定的ListBox中覆盖此DataTemplate? [英] DataTemplate for a DataType - how to override this DataTemplate in a particular ListBox?

查看:136
本文介绍了DataType的DataTemplate-如何在特定的ListBox中覆盖此DataTemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的宠物项目中的某些数据类型创建了多个数据模板。
这些数据模板非常酷,因为它们就像魔术一样工作,可以随时随地在UI中神奇地变换数据类型实例的外观。
现在,我希望能够在一个特定的ListBox中更改这些DataTypes的DataTemplate。这是否意味着我必须停止依赖WPF自动将数据模板应用于数据类型,并将ax:Key分配给DataTemplates,然后使用该密钥在UI中应用Template / ItemTemplate?

I have created several DataTemplates for some of the DataTypes in my pet project. These data templates are really cool as they work like magic, magically transforming the look of the instances of the data types whenever and wherever they show up in the UI. Now I want to be able to change the DataTemplate for these DataTypes in one particular ListBox. Does this mean I have to stop relying on WPF automatically applying the data template to the data types and assign a x:Key to the DataTemplates and then apply the Template/ItemTemplate in the UI using that key?

一个ListBox包含各种DataTypes的项(全部从一个公共基类派生),并且现在,在没有指定TemplateSelector的情况下,所有操作都可以神奇地工作,因为正确的模板是由该项的实际数据类型选择的在列表框中。如果我使用x:Key来应用DataTemplates,那么我是否需要编写TemplateSelector?

A ListBox contains items of various DataTypes (all derived from a common base class) and as it is now, all magically works without specifying a TemplateSelector, as the correct template is chosen by the actual data type of the item in the listBox. If I went for using the x:Key to apply DataTemplates, would I then need to write a TemplateSelector?

我对此并不陌生,只尝试使用DataTemplates。我想了一会,哇,多么酷!然后我想在不同的列表框中使用相同的数据类型使用不同的数据模板,哎呀,我做不到:-)请帮忙吗?

I am new to this and only experimenting with DataTemplates. One moment I think, wow, how cool! And then I want a different data template for the same data type in a different list box and ooops, I can't do it :-) Help please?

推荐答案

您可以为 ListBox 指定一个 ItemTemplate

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- your template here -->
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

或者,如果您已经定义了 DataTemplate ResourceDictionary 的某个地方:

Or alternatively, if you have already defined your DataTemplate in a ResourceDictionary somewhere:

<DataTemplate x:Key="MyTemplate">
      <!-- your template here -->
</DataTemplate>

然后您可以在 ListBox 上引用它使用:

Then you can reference it on the ListBox using:

<ListBox ItemTemplate="{StaticResource MyTemplate}" />

您无需为这两种工作方式编写模板选择器

You do not need to write a template selector for either of these approaches to work

示例以回应评论

示例下面的示例演示为窗口的数据类型(在本例中为 String )定义默认的 DataTemplate ,然后覆盖它在列表框中:

The example below demonstrates defining a default DataTemplate for a data type (in this case, String) for a window and then overridding it within a listbox:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type sys:String}">
            <Rectangle Height="10" Width="10" Margin="3" Fill="Red" />
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Rectangle Height="10" Width="10" Margin="3" Fill="Blue" />
                </DataTemplate>
            </ListBox.ItemTemplate>

            <sys:String>One</sys:String>
            <sys:String>Two</sys:String>
            <sys:String>Three</sys:String>
        </ListBox>
    </Grid>
</Window>

这将产生以下UI:

这篇关于DataType的DataTemplate-如何在特定的ListBox中覆盖此DataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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