我正在为WPF中的对话框实现TabControl
。默认情况下,所选选项卡(鼠标向下)的颜色为白色。我想将所选标签的颜色更改为悬停颜色(当我将鼠标悬停在标签上时,标签的颜色变为Office蓝色渐变,这就是我想要所选标签的颜色鼠标单击)。
我该怎么做?
这段代码不起作用:
<Style x:Key="StyleTabControl" TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="#FFFDFDFD"/>
<Style.Triggers>
<Trigger Property="IsSelected " Value="True">
<Setter Property="Background" Value="SlateGray"></Setter>
</Trigger>
</Style.Triggers>
</Style>
注意:我还尝试了触发器属性的IsMouseCaptured事件。仍然无效。
答案 0 :(得分:12)
好吧......经过几个小时的尝试,我意识到TabItem选择行为是在模板级别定义的。所以,如果我改变背景颜色,我会这样做:
<Window.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border>
<Grid>
<Grid>
<Border x:Name="border"
CornerRadius="3,3,0,0"
Background="WhiteSmoke"/>
</Grid>
<ContentPresenter ContentSource="Header"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="border"
Property="Background"
Value="LightGray" />
</Trigger>
<Trigger Property="IsSelected"
Value="True">
<Setter TargetName="border"
Property="Background"
Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>