【问题标题】:DataTemplate for a DataType - how to override this DataTemplate in a particular ListBox?DataType 的 DataTemplate - 如何在特定的 ListBox 中覆盖此 DataTemplate?
【发布时间】:2010-11-02 10:43:25
【问题描述】:

我为我的宠物项目中的一些数据类型创建了几个数据模板。 这些数据模板真的很酷,因为它们像魔术一样工作,无论何时何地出现在 UI 中,都会神奇地改变数据类型实例的外观。 现在我希望能够在一个特定的 ListBox 中更改这些 DataTypes 的 DataTemplate。这是否意味着我必须停止依赖 WPF 自动将数据模板应用于数据类型并将 x:Key 分配给 DataTemplates,然后使用该键在 UI 中应用 Template/ItemTemplate?

一个 ListBox 包含各种 DataTypes 的项目(都派生自一个公共基类),就像现在一样,所有的项目都可以在没有指定 TemplateSelector 的情况下神奇地工作,因为正确的模板是由 listBox 中项目的实际数据类型选择的.如果我使用 x:Key 来应用 DataTemplates,我是否需要编写一个 TemplateSelector?

我是新手,只尝试使用 DataTemplates。一瞬间我想,哇,好酷!然后我想在不同的列表框中为相同的数据类型使用不同的数据模板,哎呀,我做不到:-) 请帮忙?

【问题讨论】:

    标签: c# wpf datatemplate itemtemplate datatemplateselector


    【解决方案1】:

    您可以专门为您的ListBox 指定一个ItemTemplate

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

    或者,如果您已经在某处的ResourceDictionary 中定义了您的DataTemplate

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

    然后您可以在ListBox 上使用:

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

    您不需要为这些方法中的任何一种编写模板选择器


    示例响应 cmets

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

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

    【讨论】:

    • 我在 ResourceDictionary 中的 DataTemplate 以特定的 DataType 为目标,WPF 将模板自动应用于该数据类型的任何实例,无论它出现在哪里,所以即使我将 ListBox 的 ItemTemplate 设置为 { x:Null},项目仍以模板形式显示。
    • 如果您将 ItemTemplate 设置为 {x:Null} 我希望它使用默认模板 - 如果您明确指定模板,您仍然会得到相同的行为吗?
    • 感谢您的帖子。昨天我设法将不同的 ItemTemplate 应用于那个“特殊”列表框中的元素,我不记得到底是什么问题,这是一些微妙的事情。但部分问题仍然存在——因为列表框中存在三种不同类型的对象。对于这些数据类型中的每一个,我都指定了一个 DataTemplate,现在,在那个特定的列表框中,我希望这些数据类型看起来不同。我可以显式设置 ItemTemplate ,然后它将覆盖前面提到的数据模板,但是,然后我只获得所有三种类型的一个数据模板..
    • 如果列表框中有多种数据类型,您需要实现模板选择器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多