本文介绍了WPF:仅将样式应用于包含其他 tabControl 的一个 tabcontrol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 wpf 应用程序中,我有一个 tabControl(父级),其中包含另一个 tabcontrol(子级).我想将一种样式应用于父 tabControl 的 tabItem 而不影响子项.我试过这个:

In my wpf application, I have a tabControl (parent) that contains another tabcontrol (child).I would like to apply a style to the tabItem of the parent tabControl without affecting the child one.I tried with this:

<TabControl x:Name="Parent" TabStripPlacement="Left"
            ItemsSource="{Binding Path=ParentTabItems, Mode=OneWay}" >
    <TabControl.Resources>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <!-- template is defined here-->
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="{x:Type TabPanel}">
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </TabControl.Resources>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <ContentPresenter>
                <ContentPresenter.Content>
                    <!--Here there is the child TabControl-->
                </ContentPresenter.Content>
            </ContentPresenter>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

但这会导致将样式也应用于子 tabControl TabItem.如何使用应用程序中定义的默认样式仅将样式应用到父 tabItem 而离开子 TabControl?

But this results in applying the style also to the child tabControl TabItem.How can I apply the style only to the parent tabItem leaving the child TabControl using the default style defined in the application?

推荐答案

您应该能够使用 TabControl.ItemContainerStyle 上设置一个命名的 Style>TabItems 的外部 TabControl.试试这个:

You should be able to use the TabControl.ItemContainerStyle to set a named Style on the TabItems of the outer TabControl. Try this:

资源中:

<Style x:Key="ItemStyle" TargetType="{x:Type TabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <!-- template is defined here-->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

...

<TabControl x:Name="Parent" TabStripPlacement="Left"
    ItemsSource="{Binding Path=ParentTabItems, Mode=OneWay}"
    ItemContainerStyle="{StaticResource ItemStyle}">
    <TabControl.Resources>
        <Style TargetType="{x:Type TabPanel}">
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </TabControl.Resources>
</TabControl>

这篇关于WPF:仅将样式应用于包含其他 tabControl 的一个 tabcontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:12