简体   繁体   English

WPF在DataTemplates中改进了ItemTemplateSelector

[英]WPF emended ItemTemplateSelector in a DataTemplates

Hey there I am new to WPF and I have a project where I want to use an ItemTemplate selector inside a DataTemplate 嘿,我是WPF的新手,我有一个项目想要在DataTemplate中使用ItemTemplate选择器

<DataTemplate x:Key="PicTemp">
...
</DataTemplate>
<DataTemplate x:key="MsgTemp">
...
</DataTemplate>
<DataTemplate x:key="PuttingItTogether">
<TextBlock Text="HeaderText" />
???<ItemTemplateSelector="{StaticResource Select Either PicTemp or MsgTemp>}"/>
</DataTemplate>

In the third Data Template how can I setup a template selctor to choose either the PicTemp or MsgTem DataTempalte? 在第三个数据模板中,如何设置模板选择器以选择PicTemp或MsgTem DataTempalte?

ItemTemplateSelector is a property of an ItemsControl. ItemTemplateSelector是ItemsControl的属性。 You need to apply a style to the ItemsControl in order to set it. 您需要将样式应用于ItemsControl才能进行设置。 And you need to set it to an instance of an ItemTemplateSelector subclass that contains logic to return the appropriate DataTemplate for each item in the ItemsControl based on some property of the item. 并且您需要将其设置为ItemTemplateSelector子类的实例,该子类包含逻辑以根据项目的某些属性为ItemsControl中的每个项目返回适当的DataTemplate。

I found this tutorial useful for learning how to correctly implement a DTS. 发现本教程对于学习如何正确实现DTS很有用。

An ItemsTemplateSelector is used for ItemsControl such as ListBox, ListView and ItemsPresenter. ItemsTemplateSelector用于ItemsControl,例如ListBox,ListView和ItemsPresenter。
It can be used to select a DataTemplate based on the items source object. 它可以用于根据项目源对象选择一个DataTemplate。 However in most cases, it is more easy to specify the DataType -property of the corresponding DataTemplate. 但是,在大多数情况下,指定相应DataTemplate的DataType -property更容易。

In your example I don't see how exactly you want to use the TemplateSelector, since you don't have an ItemsControl . 在您的示例中,由于您没有ItemsControl ,因此我看不到要使用TemplateSelector的确切程度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM