在tabControl中为TabItem使用数据模板 [英] Using a datatemplate for the TabItems in a tabControl

查看:150
本文介绍了在tabControl中为TabItem使用数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个名为GuiObject的类,并且该类有一个名为 GuiObjects的GuiObjects列表。

If I have a class called: GuiObject, and that class has a list of GuiObjects called: "GuiObjects".

现在说我的窗口有一个GuiObjects列表,我在.xaml文件中使用它来将数据绑定到:

Now say my window has a list of GuiObjects, which I use in the .xaml file to dataBind to:

<StackPanel>
    <ItemsControl ItemsSource="{Binding TopObjectList}" DataTemplateSelector="{DynamicResource templateSelector"/> 
</StackPanel>

我可以为要生成的每种FrameworkElement制作数据模板,但是遇到了麻烦使用TabControl。我可以像这样为tabControl创建数据模板:

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:

<DataTemplate x:key="TabControlTemplate" DataTemplateSelector="{DynamicResource templateSelector" >
    <TabControl ItemsSource="{Binding GuiObjects}" />
</DataTemplate>

结果是一个制表符控件,其中存在每个正确的页面,但没有内容各个TabItem。公平地说,我将为TabItems创建一个DataTemplate。对于每个TabItem,我想将GuiObjects的内容放入堆栈面板。

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.

<DataTemplate x:key="TabItemTemplate" DataTemplateSelector="{Resource templateSelector">
    <TabItem Header = {Binding Title}>
        <StackPanel>
            <ItemsControl ItemsSource="{Binding GuiObjects}" DataTemplateSelector="{DynamicResource templateSelector"/> 
        </StackPanel>
    </TabItem>
</DataTemplate>

这里的问题是TabItemTemplate永远不会被调用。我尝试过涉及在TabControlTemplate中设置ItemContainerStyle的解决方案,但后来遇到了层次结构问题。如果我在TabItem的内容内绑定 GuiObjects,那么我绑定的是TabItems列表,而不是每个TabItem内的列表。 (我想做第二个)。例如:

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:

<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>

同样,此解决方案存在级别问题:当我说:{Binding GuiObjets}时,我指的是

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.

解决方案是要么为TabControl和TabItem都坚持使用单独的DataTemplates,然后将其添加到TabItems列表中,而不是每个TabItem中的FrameworkElements列表中。只需对其进行修复,以使DataTemplateSelector实际上可用于TabItems(不知道如何执行此操作)。或者使用ItemContainerStyle,并在绑定GuiObjects时以某种方式告诉它降低一级。有人知道该怎么做吗?

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 ,请使用以下属性:

To provide a template for the contents of the pages of a TabControl, use the following properties:

  • ContentTemplate
  • ContentTemplateSelector

ItemTemplate / ItemTemplateSelect TabControl 属性用于定义选项卡标题的外观。

The ItemTemplate/ItemTemplateSelector properties of a TabControl are used to define what the tab headers look like.

这篇关于在tabControl中为TabItem使用数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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