WPF: disable property inheritance

I would like to use TabControl as the main navigation in the application I'm working on. Therefore, I would like to make the font in the TabItems headers larger and also give it a different background color. However, I do not want this to be inherited. For example, if I use this code:

<TabControl FontSize="18pt"> <TabItem Header="Tab 1"> <Button>Button 1</Button> </TabItem> </TabControl> 

The font in the button is also large. I know that this is the normal behavior of a dependency property because the property is inherited, but that is not what I want in this case. I would like to modify TabItems without changing anything in the children. Is it impossible? Since resetting all child defaults is PITA.

Thank you for your time.

+4
source share
3 answers

Define Header as an explicit control (e.g. TextBlock or Label ) on which you apply the style:

 <TabControl FontSize="18pt"> <TabItem> <TabItem.Header> <TextBlock Style="{StaticResource tabHeaderStyle}">Tab 1</TextBlock> </TabItem.Header> <Button>Button 1</Button> </TabItem> </TabControl> 
+6
source

You need to rethink this a bit. You cannot just say “Do not inherit” because the control must inherit its property values ​​somewhere.

It works:

  <TabControl x:Name="Test" FontSize="36"> <TabControl.Resources> <Style TargetType="Button"> <Setter Property="FontSize" Value="{Binding ElementName=Test, Path=FontSize}"/> </Style> </TabControl.Resources> <TabItem Header="Test" FontSize="24"> <Button>Another test</Button> </TabItem> </TabControl> 
+1
source

Example template headers for each state: The font properties of the header content do not apply to the contents of the TabControl.

 <Style TargetType="{x:Type TabItem}"> <Setter Property="HeaderTemplate" Value="{DynamicResource TabItemGeneralTemplate.Normal}"/> <Style.Triggers> <Trigger Property="Selector.IsSelected" Value="True"> <Setter Property="Background" Value="White"/> <Setter Property="HeaderTemplate" Value="{DynamicResource TabItemGeneralTemplate.Selected}"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True"/> <Condition Property="Selector.IsSelected" Value="False"/> </MultiTrigger.Conditions> <Setter Property="Background" Value="White"/> <Setter Property="HeaderTemplate" Value="{DynamicResource TabItemGeneralTemplate.Hover}"/> </MultiTrigger> </Style.Triggers> </Style> ... <DataTemplate x:Key="TabItemGeneralTemplate.Selected"> <Label Content="{Binding}" Foreground="#FF343434" FontWeight="Bold"/> </DataTemplate> <DataTemplate x:Key="TabItemGeneralTemplate.Hover"> <Label Content="{Binding}" Foreground="{DynamicResource Background.Primary}"/> </DataTemplate> <DataTemplate x:Key="TabItemGeneralTemplate.Normal"> <Label Content="{Binding}" Foreground="#FF7C7C7C"/> </DataTemplate> 
0
source

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


All Articles