How to change the color of the selected tab in TabControl?

I am implementing TabControl for a dialog box in WPF. The color of the selected tab (mouse-down) is white by default. I want to change the color of this selected tab to a hover color (when I hover over the tab, the color of the tab changes to a blue-gradient Office, and this is what I want the color of the selected tab to be with a click of the mouse).

How can i do this?

This piece of code does not work:

 <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> 

Note. I also tried the IsMouseCaptured event for the trigger property. Still not working.

+4
source share
1 answer

Ok ... after several hours of trying, I realized that the TabItem selection behavior is defined at the template level. So, if I change the color of backgrnd, I do this:

 <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> 
+9
source

Source: https://habr.com/ru/post/1499187/


All Articles