Maximilian Csuk
Maximilian Csuk

Reputation: 594

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.

Upvotes: 6

Views: 4866

Answers (3)

David Menoud
David Menoud

Reputation: 71

Example of templated Headers for each states: The fonts properties of the header content are not propaged into the TabControl content.

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

Upvotes: 0

Robert Rossney
Robert Rossney

Reputation: 96920

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>

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292765

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>

Upvotes: 7

Related Questions