DataTemplate for a DataType - how to override this DataTemplate in a particular ListBox?

荒凉一梦 提交于 2020-01-02 06:58:31

问题


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?

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?

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?


回答1:


You can specify an ItemTemplate specifically for your ListBox:

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

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

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

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


Example in response to comments

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>

This produces the following UI:



来源:https://stackoverflow.com/questions/4076884/datatemplate-for-a-datatype-how-to-override-this-datatemplate-in-a-particular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!