WPF:TabControl.ItemTemplate 和 TabItem.ContentTemplate 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25265266/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:19:26  来源:igfitidea点击:

WPF:Difference between TabControl.ItemTemplate and TabItem.ContentTemplate

c#wpf

提问by Andrew Carl

I'm confused on this for a long time,these both seem to affect the tabitems' presentation in the tabcontrol. Is it designed for best control of the presentation of the tabcontrol? Or if there is something I dont't understand.

我很长时间对此感到困惑,这些似乎都影响了 tabitems 在 tabcontrol 中的显示。它是为最好地控制 tabcontrol 的呈现而设计的吗?或者如果有什么我不明白的。

采纳答案by Sheridan

The ItemsControl.ItemTemplateProperty is used to define what each item in the data bound collection should look like... from the ItemsControl.ItemTemplatePropertypage on MSDN:

ItemsControl.ItemTemplate属性用于定义数据绑定集合中的每个项目应该是什么样子......来自MSDN 上的ItemsControl.ItemTemplate属性页面:

Gets or sets the DataTemplate used to display each item.

获取或设置用于显示每个项目的 DataTemplate。

As you can see, it is of type DataTemplate, which is customary for a template that displays data... its DataContextwill automatically be set to an item from the collection and so controls declared in that DataTemplatewill automatically have access to the items properties. Please see the Data Templating Overviewpage on MSDN for further help with this.

如您所见,它是 type DataTemplate,这是显示数据的模板的习惯用法......它DataContext会自动设置为集合中的一个项目,因此在其中声明的控件DataTemplate将自动访问项目属性。请参阅MSDN 上的数据模板概述页面以获得更多帮助。

Similarly, from MSDN, the ContentControl.ContentTemplateProperty:

同样,从 MSDN,ContentControl.ContentTemplate属性

Gets or sets the data template used to display the content of the ContentControl.

获取或设置用于显示 内容的数据模板ContentControl

Again, its DataContextwill automatically be set to the object that is set as the Contentproperty. Please note that the ContentControlonly has a ContentTemplateProperty and no ItemTemplateProperty, which is used for collection items... from the Data Templating Overview page on MSDN:

同样,它DataContext会自动设置为设置为Content属性的对象。请注意,ContentControl只有一个ContentTemplate属性,没有ItemTemplate属性,用于收集项目...来自 MSDN 上的数据模板概述页面:

Because myTaskTemplate is a resource, you can now use it on other controls that have a property that takes a DataTemplate type. As shown above, for ItemsControl objects, such as the ListBox, it is the ItemTemplate property. For ContentControl objects, it is the ContentTemplate property.

由于 myTaskTemplate 是一种资源,您现在可以在具有采用 DataTemplate 类型的属性的其他控件上使用它。如上所示,对于 ItemsControl 对象,例如 ListBox,它是 ItemTemplate 属性。对于 ContentControl 对象,它是 ContentTemplate 属性。



UPDATE >>>

更新 >>>

To clarify this situation further, think of this simple rule:

为了进一步澄清这种情况,请考虑以下简单规则:

Use the ContentTemplateproperty to define how an object that is set as the Contentproperty of a ContentControlshould look.

Use the ItemTemplateproperty to define how the itemsof a collection control should look.

使用ContentTemplate属性来定义设置为Content属性的对象的ContentControl外观。

使用该ItemTemplate属性来定义集合控件的项目的外观。

That the difference at its simplest. However, I'd like to point out that as these properties are both of type DataTemplate, their values areinterchangeable.

最简单的区别。但是,我想指出,由于这些属性都是 type DataTemplate,因此它们的值可以互换。

For example, let's say that you have a Personclass and you display a collection of Personobjects in a ListBox. You can declare a DataTemplateto set as the ListBox.ItemTemplateproperty to define how each Personin the collection should look. However, if you just wanted to display a single Person, then you could use a ContentControlwith the Contentset to an instance of the Personclass, and still use the sameDataTemplate, but set as the ContentTemplateinstead:

例如,假设您有一个Person类并且您PersonListBox. 您可以声明一个DataTemplateto set 作为ListBox.ItemTemplate属性来定义Person集合中的每个元素的外观。但是,如果您只想显示单个Person,那么您可以将 aContentControlContentset 一起用于Person类的实例,并且仍然使用相同的DataTemplate,但将其设置为ContentTemplate

Multiple objects:

多个对象:

<ListBox ItemsSource="{Binding People}" ItemTemplate="{StaticResource Template}" ... />

...

...

Single object:

单个对象:

<ContentControl Content="{Binding Person}" 
    ContentTemplate="{StaticResource Template}" ... />

回答by Nathan Phillips

There's some very long answers here for what is actually a very simple question. To avoid confusion:

对于实际上非常简单的问题,这里有一些很长的答案。为避免混淆:

ItemTemplateis the template used to format each item in the ItemsSourceto create the headers (the controls that appear in the tab bar) and ContentTemplateis the template used to format each item in the ItemsSourceto create the content of the tabs (the controls that appear when you click on the header).

ItemTemplate是用于格式化每个项目ItemsSource以创建标题(出现在标签栏中的控件)ContentTemplate的模板,是用于格式化每个项目ItemsSource以创建标签内容的模板(单击时出现的控件)在标题上)。

回答by Roel van Westerop

Setting the TabControl.ItemTemplateyou specify a template to use for all TabItemsin the Itemscollection of the TabControl, unless you override the TabItem.ContentTemplatefor a specific TabItem.

设置TabControl.ItemTemplate你指定一个模板用于所有TabItems的在Items收集TabControl,除非您覆盖TabItem.ContentTemplate特定TabItem

So, while they do the same, TabControl.ItemTemplateis a more generic template for all the TabItemsin the TabControland TabItem.ContentTemplateis specific for the TabItemit is used in.

因此,虽然它们执行相同的操作,但它TabControl.ItemTemplate是一个更通用的模板,适用于所有TabItemsinTabControl并且TabItem.ContentTemplate特定于TabItem它所使用的。

The above is not quite true, as TabControlhas an ItemTemplateproperty and a ContentTemplateproperty, to make it more confusing.

以上并不完全正确,因为TabControl有一个ItemTemplate属性和一个ContentTemplate属性,使其更加混乱。

ItemTemplateis used as template for the header (the tab thingy) of all TabItemsadded through databinding on the ItemsSourceor through Xaml without making the the added item a TabItem:

ItemTemplate用作通过 XamlTabItems上的数据绑定ItemsSource或通过 Xaml添加的所有标头(选项卡)的模板,而不使添加的项目 a TabItem

<TabControl ItemsSource="{Binding ListOfItems}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red"/>
        </DataTemplate>
     </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
             <TextBlock Text="{Binding}" Foreground="Blue"/>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

This will create a TabControlwith red text in the header/tab and blue text for content.

这将TabControl在标题/选项卡中创建一个红色文本,内容为蓝色文本。

Now, if we do the following:

现在,如果我们执行以下操作:

<TabControl>
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red"/>
        </DataTemplate>
     </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
             <TextBlock Text="{Binding}" Foreground="Blue"/>
        </DataTemplate>
    </TabControl.ContentTemplate>

    <TabItem Header="One" Content="One"/>
    <TabItem Header="Two" Content="Two"/>
    <TabItem Header="Three" Content="Three"/>
</TabControl>

We'll have a TabControlwith three tabs, and the header text is black, content is still blue. And a DataError informing us that the ItemTemplateand ItemTemplateSelectorproperties are ignored for items already of the ItemsControl'scontainer type, in this case TabItem. In this case, we need to specify TabItem.HeaderTemplateto change the appearance of the header.

我们将有一个TabControl包含三个选项卡的选项卡,标题文本为黑色,内容仍为蓝色。还有一个 DataError 通知我们,对于已经属于容器类型的项目,在这种情况下,将忽略ItemTemplateItemTemplateSelector属性。在这种情况下,我们需要指定更改标题的外观。ItemsControl'sTabItemTabItem.HeaderTemplate

So TabControl.ItemTemplateand TabItem.ContentTemplatedon't do the same, but my previous explanation still holds for TabControl.ContentTemplateand TabItem.ContentTemplate.

所以TabControl.ItemTemplateTabItem.ContentTemplate不要这样做,但我之前的解释仍然适用于TabControl.ContentTemplateand TabItem.ContentTemplate