更改主题 tabitem 的选定背景颜色
我正在尝试做一些我认为很简单的事情,但结果却相当复杂。 我正在使用一个 TabControl,该 TabControl 已通过包含的程序集中的主题进行样式设置,但我希望在选择 TabItem 时 TabItem 的背景颜色有所不同。虽然我可以在其他程序集中的主题上更改此设置,但我宁愿为我的应用程序覆盖它。
问题似乎是为选定的 TabItem 设置背景颜色涉及设置控件模板,特别是 IsSelected 属性上的控件模板触发器。我找不到一种简单的方法来只覆盖一个触发器而不像这样完全定义控制模板(导致一堆不必要的复制/粘贴代码)。这就是我现在所拥有的 - 有没有办法减少它以仅针对我关心的部分? ControlTemplates 似乎不可继承。感谢您提供的任何帮助。
<Style TargetType="TabItem" BasedOn="{StaticResource {x:Type TabItem}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border"
Margin="0,0,-4,0"
Background="{DynamicResource {x:Static themes:HarmonyBrushes.BrushDKey}}"
BorderBrush="{DynamicResource {x:Static themes:HarmonyBrushes.BrushDKey}}"
BorderThickness="1,1,1,1"
CornerRadius="3,3,0,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,2,12,2"
RecognizesAccessKey="True" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="100" />
<Setter TargetName="Border"
Property="Background"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushLightKey}}" />
<Setter TargetName="Border"
Property="BorderBrush"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushLightKey}}" />
<Setter Property="Foreground"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.TextBrushLightKey}}" />
<Setter TargetName="Border"
Property="BorderThickness"
Value="1,1,1,0" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border"
Property="Background"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushDarkKey}}" />
<Setter TargetName="Border"
Property="BorderBrush"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushDarkKey}}" />
<Setter Property="Foreground"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.TextBrushLightKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'm trying to do something which I'd think would be pretty simple, but is turning out to be rather complicated.
I'm working with a TabControl that has already been styled by a theme in an included assembly, but I'd like the background color of a TabItem when the TabItem is selected to be different. While I could change this on the theme in the other assembly, I'd rather just override it for my application.
The problem seems to be that setting the background color for selected TabItems involves setting a control template, and in particular, a control template trigger on the IsSelected property. I can't find an easy way to only override that one trigger without fully defining the control template like so (resulting in a bunch of unnecessary copy/pasted code). Here's what I have right now- is there a way to cut this down to target only the part I care about? It doesn't seem like ControlTemplates are inheritable. Thanks for any help you can offer.
<Style TargetType="TabItem" BasedOn="{StaticResource {x:Type TabItem}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border Name="Border"
Margin="0,0,-4,0"
Background="{DynamicResource {x:Static themes:HarmonyBrushes.BrushDKey}}"
BorderBrush="{DynamicResource {x:Static themes:HarmonyBrushes.BrushDKey}}"
BorderThickness="1,1,1,1"
CornerRadius="3,3,0,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,2,12,2"
RecognizesAccessKey="True" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="100" />
<Setter TargetName="Border"
Property="Background"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushLightKey}}" />
<Setter TargetName="Border"
Property="BorderBrush"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushLightKey}}" />
<Setter Property="Foreground"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.TextBrushLightKey}}" />
<Setter TargetName="Border"
Property="BorderThickness"
Value="1,1,1,0" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border"
Property="Background"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushDarkKey}}" />
<Setter TargetName="Border"
Property="BorderBrush"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushDarkKey}}" />
<Setter Property="Foreground"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.BrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static themes:HarmonyBrushes.TextBrushLightKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为更改颜色最简单的方法是覆盖资源:
I think the simplest way to change the colour is to override the resource: