Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: disable inheritance of properties

I would like to use a TabControl as the main navigation in the application I am working on. So I would like to make the font in the headers of the TabItems bigger and also give it another 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 18pt big. I know that this is normal dependency property behaviour because the property is inherited, but that's not what I want in this case. I would like to change the TabItems without changing anything in the children. Isn't that possible? Because re-setting all children to default values is a PITA.

Thanks for your time.

like image 480
Maximilian Csuk Avatar asked Mar 15 '10 18:03

Maximilian Csuk


2 Answers

Define the Header as an explicit control (TextBlock or Label for instance), on which you apply a style :

<TabControl FontSize="18pt">
  <TabItem>
    <TabItem.Header>
        <TextBlock Style="{StaticResource tabHeaderStyle}">Tab 1</TextBlock>
    </TabItem.Header>
    <Button>Button 1</Button>
  </TabItem>
</TabControl>
like image 98
Thomas Levesque Avatar answered Sep 22 '22 00:09

Thomas Levesque


You have to rethink this a bit. You can't just say "Don't inherit," because the control has to inherit its property values from somewhere.

This 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>
like image 41
Robert Rossney Avatar answered Sep 22 '22 00:09

Robert Rossney