Changing tab background active in C # and XAML

Ok, I searched everywhere, and in every single link I get to my problem, explained by xaml code.

I want to change the active background of the tab and the foreground (not its contents, but the top that you select to activate) in the WPF project , but I'm looking for C # code. The code below does not work for me:

 if (tabs[0].IsEnabled) tabs[0].Background = Brushes.Blue; else tabs[0].Background = Brushes.Black; 
+4
source share
3 answers

Do it in XAML if you are using WPF.

You can bind to the TabControl ItemsSource property. Then just select Styletrigger to change the background.

+4
source

OK, thanks Venson I finally got it, and just in case someone wants to know how this works:

 <TabControl ItemsSource="{Binding tabs}" Height="68" HorizontalAlignment="Left" Margin="156,23,0,0" Name="tabControl1" VerticalAlignment="Top" Width="268"> <TabControl.ItemContainerStyle> <Style TargetType="TabItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Grid> <Border Margin="0,0,-4,0" Background="Black" BorderBrush="Blue" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" > <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="Foreground" Value="Blue"></Setter> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabItem}"> <Grid> <Border Margin="0,0,-4,0" Background="Green" BorderBrush="Blue" BorderThickness="1,1,1,1" CornerRadius="2,12,0,0" > <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"/> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Trigger> </Style.Triggers> </Style> </TabControl.ItemContainerStyle> </TabControl> 

This code is in the <Grid> tags of the <Window> MainWindow.xaml and

 public MainWindow() { testClass testObject = new testClass(); testObject.tabs = new List<TabItem>(); testObject.tabs.Add(new TabItem()); testObject.tabs.Add(new TabItem()); testObject.tabs[0].Header = "NO WAY"; testObject.tabs[1].Header = "ON WAY"; testObject.tabs[0].Content = "WHAT"; testObject.tabs[1].Content = "HELL"; InitializeComponent(); this.DataContext = testObject ; } class testClass { public List<TabItem> tabs { set; get; } } 

this is included in the MainWindow.xaml.cs file.

Please note: the colors are for test purposes only, do not judge me for the selected poor contrast.

I don’t know if it is possible to use another class, but ..

+2
source
 foreach(var tab in tabs) { tab.Background = tab.IsEnabled ? Brushes.Blue :Brushes.Black; } 

But you can handle the state of tabcontrol with active changes to tabs and set the backgrounds of deactivated and activated tabs.

0
source

All Articles