wpf 将数据模板用于 tabControl 中的 TabItems
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20939660/
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
Using a datatemplate for the TabItems in a tabControl
提问by NickLokarno
If I have a class called: GuiObject, and that class has a list of GuiObjects called: "GuiObjects".
如果我有一个名为:GuiObject 的类,并且该类有一个名为“GuiObjects”的 GuiObjects 列表。
Now say my window has a list of GuiObjects, which I use in the .xaml file to dataBind to:
现在说我的窗口有一个 GuiObjects 列表,我在 .xaml 文件中使用它来数据绑定到:
<StackPanel>
<ItemsControl ItemsSource="{Binding TopObjectList}" DataTemplateSelector="{DynamicResource templateSelector"/>
</StackPanel>
I can make a datatemplate for every type of FrameworkElement I want to generate, but I'm having trouble with the TabControl. I can create a datatemplate for the tabControl like so:
我可以为我想要生成的每种 FrameworkElement 类型制作一个数据模板,但是我在使用 TabControl 时遇到了问题。我可以为 tabControl 创建一个数据模板,如下所示:
<DataTemplate x:key="TabControlTemplate" DataTemplateSelector="{DynamicResource templateSelector" >
<TabControl ItemsSource="{Binding GuiObjects}" />
</DataTemplate>
And the result is a tab control that has each of the proper pages present, but without the contents of the individual TabItems. Fair enough, I'll just make a DataTemplate for the TabItems. For each TabItem, I'd like to put the contents of GuiObjects into a stackpanel.
结果是一个选项卡控件,其中包含每个正确的页面,但没有单个 TabItems 的内容。公平地说,我将为 TabItems 创建一个 DataTemplate。对于每个 TabItem,我想将 GuiObjects 的内容放入堆栈面板中。
<DataTemplate x:key="TabItemTemplate" DataTemplateSelector="{Resource templateSelector">
<TabItem Header = {Binding Title}>
<StackPanel>
<ItemsControl ItemsSource="{Binding GuiObjects}" DataTemplateSelector="{DynamicResource templateSelector"/>
</StackPanel>
</TabItem>
</DataTemplate>
The problem here is that the TabItemTemplate never gets called. I've tried solutions that involve setting the ItemContainerStyle within the TabControlTemplate, but then I've got the problem of hierarchy. If I bind "GuiObjects" inside the content of the TabItem, I'm binding the list of tabItems, instead of the list that's within each TabItem. (I want to do the second one). Here's an example:
这里的问题是 TabItemTemplate 永远不会被调用。我尝试过涉及在 TabControlTemplate 中设置 ItemContainerStyle 的解决方案,但后来我遇到了层次结构问题。如果我在 TabItem 的内容中绑定“GuiObjects”,我将绑定 tabItem 列表,而不是每个 TabItem 中的列表。(我想做第二个)。下面是一个例子:
<DataTemplate x:key="TabControlTemplate" DataTemplateSelector="{DynamicResource templateSelector" >
<TabControl ItemsSource="{Binding GuiObjects}">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding Title}"/>
<Setter Property="Content" Value="<StackPanel><ItemsControl ItemsSource="{Binding GuiObjects}" DataTemplateSelector="{DynamicResource templateSelector"/></StackPanel>"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</DataTemplate>
Again, this solution has the levels problem: When I say: {Binding GuiObjets} I'm referring to the list of TabItems, instead of to the list of FrameworkElements within each TabItem.
同样,此解决方案存在级别问题:当我说:{Binding GuiObjets} 时,我指的是 TabItem 列表,而不是每个 TabItem 中的 FrameworkElement 列表。
The solution is either to stick with separate DataTemplates for both the TabControl and the TabItem, and just fix it so that the DataTemplateSelector actually works for the TabItems (no idea how to do this). Or to go with the ItemContainerStyle, and somehow tell it to go down one level when binding GuiObjects. Anyone know how to do this?
解决方案是为 TabControl 和 TabItem 坚持使用单独的 DataTemplates,然后修复它,以便 DataTemplateSelector 实际上适用于 TabItems(不知道如何做到这一点)。或者使用 ItemContainerStyle,并在绑定 GuiObjects 时以某种方式告诉它下降一级。有人知道怎么做吗?
回答by O. R. Mapper
To provide a template for the contentsof the pages of a TabControl, use the following properties:
要为 a的页面内容提供模板TabControl,请使用以下属性:
The ItemTemplate/ItemTemplateSelectorproperties of a TabControlare used to define what the tab headers look like.
a 的ItemTemplate/ItemTemplateSelector属性TabControl用于定义选项卡标题的外观。

