DataTemplate 与 ItemContainerTemplate [英] DataTemplate vs ItemContainerTemplate

查看:65
本文介绍了DataTemplate 与 ItemContainerTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ItemContainerTemplate 有什么用?它派生自 DataTemplate,但除了 ItemContainerTemplateKey 属性外,我看不出它们之间有任何区别.我什么时候应该用一个,什么时候用另一个?

解决方案

DataTemplateItemContainerTemplate 的唯一区别是资源字典键的自动提供方式(假设它没有明确设置).即DataTemplate[DictionaryKeyProperty("DataTemplateKey")]属性修饰,DataTemplateKey基本定义为:

公共对象DataTemplateKey{得到 { 返回(数据类型!= null)?新数据模板键(数据类型):空;}

请参阅 DataTemplate 源以供参考.

ItemContainerTemplate 派生自 DataTemplate,但用 [DictionaryKeyProperty("ItemContainerTemplateKey")] 属性修饰(实际上取代了继承的),ItemContainerTemplateKey 属性定义如下:

公共对象ItemContainerTemplateKey{得到 { 返回(数据类型!= null)?新 ItemContainerTemplateKey(DataType) : null;}}

请参阅ItemContainerTemplate 源以供参考.

差异似乎很小 - DataTemplate 返回 DataTemplateKey 的实例,ItemContainerTemplate 返回 ItemContainerTemplateKey 的实例(两者都源自 TemplateKey).所以基本上这两个是等价的1:

<DataTemplate x:Key="{ItemContainerTemplateKey {x:Type sys:String}}"/>

还有这些:

<DataTemplate DataType="{x:Type sys:String}"/>

这两者之间的主要实际区别在于具有默认键的DataTemplate被视为隐式模板2,而 ItemContainerTemplate 不是.事实上,你需要手动引用它,例如:

<ListBox ItemTemplate="{StaticResource {ItemContainerTemplate {x:Type sys:String}}}"/>

我不确定创建 ItemContainerTemplate 类背后的意图.我想它可以让您更清楚地了解代码,您知道这样的模板专门用于 ItemsControl(或派生控件).另外,我想编写一个可充分利用此类的强可重用 DataTemplateSelector 会被证明是非常简单的.

<小时>

1 在创建的对象具有不同类型的意义上,它们并不等效,但在功能上它们是等效的.>

2 隐式模板应用于作用域内所有对应类型的对象,除非显式设置模板.

What is ItemContainerTemplate used for? It is derived from DataTemplate, but I don't see any difference between them except the ItemContainerTemplateKey property. When should I use one and when the other?

解决方案

The only difference between DataTemplate and ItemContainerTemplate is the way the resource dictionary key is automatically provided (assuming it is not set explicitly). Namely, DataTemplate is decorated with [DictionaryKeyProperty("DataTemplateKey")] attribute, and the DataTemplateKey is basically defined as:

public object DataTemplateKey
{
    get { return (DataType != null) ? new DataTemplateKey(DataType) : null; 
}

See DataTemplate source for reference.

ItemContainerTemplate derives from DataTemplate, but is decorated with [DictionaryKeyProperty("ItemContainerTemplateKey")] attribute (which in practice replaces the inherited one), and ItemContainerTemplateKey property is defined as follows:

public object ItemContainerTemplateKey
{
    get { return (DataType != null) ? new ItemContainerTemplateKey(DataType) : null; }
}

See ItemContainerTemplate source for reference.

The difference seems small - DataTemplate returns an instance of DataTemplateKey and ItemContainerTemplate returns an instance of ItemContainerTemplateKey (both derive from TemplateKey). So basically these two are equivalent1:

<ItemContainerTemplate DataType="{x:Type sys:String}" />
<DataTemplate x:Key="{ItemContainerTemplateKey {x:Type sys:String}}" />

and so are these:

<ItemContainerTemplate x:Key="{DataTemplateKey {x:Type sys:String}}" />
<DataTemplate DataType="{x:Type sys:String}" />

The main practical difference between these two is that DataTemplate with default key is treated as an implicit template2, whereas ItemContainerTemplate is not. In fact, you need to manually reference it, e.g.:

<ListBox ItemTemplate="{StaticResource {ItemContainerTemplate {x:Type sys:String}}}" />

I'm not sure about the intentions behind creating ItemContainerTemplate class. I guess it gives you a clearer overview of the code, where you know that such a template is specifically intended to be used in an ItemsControl (or a derived control). Also, I guess it would prove to be pretty simple to write a strongly reusable DataTemplateSelector that would take advantage of this class.


1 They're not equivalent in the sense that created objects are of different types, but functionally they're equivalent.

2 Implicit templates are applied to all objects of corresponding type within the scope, unless a template is set explicitly.

这篇关于DataTemplate 与 ItemContainerTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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