wpf DataTemplate 与 ItemContainerTemplate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23824607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTemplate vs ItemContainerTemplate
提问by andreas
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?
ItemContainerTemplate 有什么用?它派生自 DataTemplate,但除了 ItemContainerTemplateKey 属性外,我看不出它们之间有任何区别。我什么时候应该使用一个,什么时候使用另一个?
回答by Grx70
The only difference between DataTemplateand ItemContainerTemplateis the way the resource dictionary key is automatically provided (assuming it is not set explicitly). Namely, DataTemplateis decorated with [DictionaryKeyProperty("DataTemplateKey")]attribute, and the DataTemplateKeyis basically defined as:
DataTemplate和之间的唯一区别ItemContainerTemplate是自动提供资源字典键的方式(假设它没有明确设置)。即,DataTemplate用[DictionaryKeyProperty("DataTemplateKey")]属性修饰,DataTemplateKey基本上定义为:
public object DataTemplateKey
{
get { return (DataType != null) ? new DataTemplateKey(DataType) : null;
}
See DataTemplate sourcefor reference.
请参阅DataTemplate 源以供参考。
ItemContainerTemplatederives from DataTemplate, but is decorated with [DictionaryKeyProperty("ItemContainerTemplateKey")]attribute (which in practice replaces the inherited one), and ItemContainerTemplateKeyproperty is defined as follows:
ItemContainerTemplate派生自DataTemplate,但用[DictionaryKeyProperty("ItemContainerTemplateKey")]属性修饰(实际上取代了继承的),并且ItemContainerTemplateKey属性定义如下:
public object ItemContainerTemplateKey
{
get { return (DataType != null) ? new ItemContainerTemplateKey(DataType) : null; }
}
See ItemContainerTemplate sourcefor reference.
请参阅ItemContainerTemplate 源以供参考。
The difference seems small - DataTemplatereturns an instance of DataTemplateKeyand ItemContainerTemplatereturns an instance of ItemContainerTemplateKey(both derive from TemplateKey). So basically these two are equivalent1:
差异似乎很小 -DataTemplate返回 的实例DataTemplateKey并ItemContainerTemplate返回 的实例ItemContainerTemplateKey(均源自TemplateKey)。所以基本上这两个是等价的1:
<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 mainpractical difference between these two is that DataTemplatewith default key is treated as an implicit template2, whereas ItemContainerTemplateis not. In fact, you need to manually reference it, e.g.:
这两者之间的主要实际区别在于DataTemplate,默认键被视为隐式模板2,而ItemContainerTemplate不是。实际上,您需要手动引用它,例如:
<ListBox ItemTemplate="{StaticResource {ItemContainerTemplate {x:Type sys:String}}}" />
I'm not sure about the intentions behind creating ItemContainerTemplateclass. 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 DataTemplateSelectorthat would take advantage of this class.
我不确定创建ItemContainerTemplate类背后的意图。我想它可以让您更清楚地了解代码,您知道这样的模板专门用于ItemsControl(或派生控件)。另外,我想编写一个DataTemplateSelector可以利用这个类的强可重用的方法会被证明是非常简单的。
1They're not equivalent in the sense that created objects are of different types, but functionally they're equivalent.
1在创建的对象属于不同类型的意义上,它们不是等价的,但在功能上它们是等价的。
2Implicit templates are applied to all objects of corresponding type within the scope, unless a template is set explicitly.
2隐式模板适用于范围内所有相应类型的对象,除非明确设置了模板。
回答by Albi
The ItemContainerTemplate describes the world around your Item. For example in a ListBox the selection rectangle around your ListBoxItem. The DataTemplate describes how you ListBoxItem apears and of which elements it consists.
ItemContainerTemplate 描述了您的 Item 周围的世界。例如,在 ListBox 中,ListBoxItem 周围的选择矩形。DataTemplate 描述了 ListBoxItem 如何出现以及它由哪些元素组成。
Dr. WPF did a good example: http://drwpf.com/blog/category/item-containers/
WPF博士做了一个很好的例子:http: //drwpf.com/blog/category/item-containers/
回答by SLaks
You can put an ItemContainerTemplatein a ResourceDictionary, and it will automatically use the DataTypeas its key.
您可以将 anItemContainerTemplate放入 a 中ResourceDictionary,它会自动使用 theDataType作为其键。
That's the only difference.
这是唯一的区别。
回答by Moez Rebai
You could check that Link to see the difference between controltemplate and datatemplate and hierarchicaldatatemplate itemspaneltemplate:
您可以检查该链接以查看 controltemplate 和 datatemplate 和hierarchicaldatatemplate itempaneltemplate 之间的区别:

