TabItem.IsSelected的触发器不起作用

时间:2016-01-30 23:12:44

标签: wpf styles selecteditem

我想将选项卡背景设置为红色并取消选择为绿色。但是,更改时,只有未选中的选项卡显示为绿色。选定的白色。

        <TabControl Background="LightGray" Name="MainTabControl">
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="False">
                        <Setter Property="Background" Value="Green" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TabControl.Resources>
        <TabItem Header="Main" />
        <TabItem Header="Optimizer" />
    </TabControl>

enter ifdsfdsre

1 个答案:

答案 0 :(得分:5)

覆盖TabItem的{​​{1}}并为其添加触发器

ControlTemplate

修改

保留原始<Style TargetType="{x:Type TabItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Grid> <Border Name="Border" > <ContentPresenter VerticalAlignment="Center" Margin="5" HorizontalAlignment="Center" ContentSource="Header" > </ContentPresenter> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="Border" Property="Background" Value="Red" /> </Trigger> <Trigger Property="IsSelected" Value="False"> <Setter TargetName="Border" Property="Background" Value="Green" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 样式并添加背景颜色,从设计师右键单击tabitem并选择EditTemplate&gt;编辑副本,然后更新负责非选定标签颜色的tabitem Gardian画笔,以及负责选定标签颜色的TabItem.Static.Background画笔:

TabItem.Selected.Background

或者您可以在 //.. <LinearGradientBrush x:Key="TabItem.Static.Background" EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#F0F0F0" Offset="0.0"/> <GradientStop Color="Green" Offset="1.0"/> </LinearGradientBrush> <SolidColorBrush x:Key="TabItem.Static.Border" Color="#ACACAC"/> <LinearGradientBrush x:Key="TabItem.MouseOver.Background" EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#ECF4FC" Offset="0.0"/> <GradientStop Color="#DCECFC" Offset="1.0"/> </LinearGradientBrush> <SolidColorBrush x:Key="TabItem.MouseOver.Border" Color="#7EB4EA"/> <SolidColorBrush x:Key="TabItem.Disabled.Background" Color="#F0F0F0"/> <SolidColorBrush x:Key="TabItem.Disabled.Border" Color="#D9D9D9"/> <SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/> <SolidColorBrush x:Key="TabItem.Selected.Background" Color="Red"/> //.. templateBinding mainBorder属性中使用innerBorder(最初只有BackgroundmainBordertemplateBinded属性),然后将触发器添加到background触发器

controlTemplate

并在结尾处添加触发器

<Grid x:Name="templateRoot" SnapsToDevicePixels="true">
                        <Border x:Name="mainBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Background="{TemplateBinding Background}" Margin="0">
                            <Border x:Name="innerBorder" BorderBrush="{StaticResource TabItem.Selected.Border}" BorderThickness="1,1,1,0"  Background="{TemplateBinding Background}" Margin="-1" Opacity="0"/>
                        </Border>
                        <ContentPresenter x:Name="contentPresenter" ContentSource="Header" Focusable="False" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                    </Grid>

那应该回答你的第二个问题