阻止子项在 TabControls 中继承父项样式 [英] Stop child from inheriting parent Style in TabControls
问题描述
在我的 WPF 应用程序中,我有一个 TabControl 绑定到我创建的样式:
在我看来:
在单独的 ResourceDictionary 上:
到目前为止一切顺利,一切都按预期进行.问题是现在这个 TabControl 的所有子项,例如 TabItem 中的 ListView,也获得与 TabControl 相同的 FontSize,而不是默认值.
我认为通过指定 TargetType="{x:Type TabControl}" 我会阻止样式应用于不同类型的子项.我正在寻找的是实际上阻止它影响除了显式继承样式的组件之外的所有内容.那么如何做到这一点呢?我想我错过了一些简单的东西...
如果我在 ListView 中覆盖字体大小,它会起作用,但这意味着我必须为每个孩子都这样做,这可能会变得非常麻烦.
我已经阅读了这个问题和其他问题,但我找不到我正在寻找的答案:
In my WPF application i have a TabControl that i am binding to a style i created:
On my View:
<TabControl Grid.Row="6" Style="{DynamicResource SideBarTabControl}">
On a separate ResourceDictionary:
<Style x:Key="SideBarTabControl" TargetType="{x:Type TabControl}" BasedOn="{StaticResource {x:Type TabControl}}" >
<Setter Property="FontSize" Value="{DynamicResource TitleFontSize}"/>
</Style>
So far so good, things work as expected. The problem is that now all the children of this TabControl, such as a ListView inside a TabItem, is also getting the same FontSize as the TabControl, instead of the default.
I thought that by specifying TargetType="{x:Type TabControl}" i would stop the style from being applied to children of different types. What i'm looking for is to actually stop it from affecting EVERYTHING BUT the component that explicitly inherited the style. So how can this be done? I think i am missing something simple...
If i override the font size in my ListView it works, but this means i have to do it for every child, which might become very cumbersome.
I have read this and other questions but i can't find the answer i'm looking for:
Is it possible to set a style in XAML that selectively affects controls?
This is working for me. The part that's doing the work is TabControl.ItemContainerStyle
. It applies a font size only to the header content.
<TabControl>
<TabControl.ItemContainerStyle>
<Style
TargetType="TabItem"
BasedOn="{StaticResource {x:Type TabItem}}"
>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<ContentControl
TextElement.FontSize="20"
Content="{Binding Header, RelativeSource={RelativeSource AncestorType=TabItem}}"
/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.ItemContainerStyle>
<TabItem Header="Foo">
<Label Content="Bar" />
</TabItem>
<TabItem Header="Baz">
<Label Content="Bar" />
</TabItem>
</TabControl>
这篇关于阻止子项在 TabControls 中继承父项样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!