views:

210

answers:

2

Hi!

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.

+3  A: 

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>
Thomas Levesque
That's pretty much what I need. I had expected this to be a bit easier but this is nonetheless what I want. Thanks!
Maximilian Csuk
+1  A: 

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>
Robert Rossney
What do you mean by "rethink"? I think my claims are pretty decent, or not? Why can't such a basic task not be made easy by WPF? XAML is cool and all but sometimes I have the feeling Microsoft sacrifices ease of use a bit too much.
Maximilian Csuk
What I meant by "rethink": your question seems to have as its premise that the button has a default font size, and that what you want to do is stop it from inheriting. But what you almost certainly actually want the is for the button to continue inheriting its properties, but from something other than its immediate parent - that way, if you change the font size on the Window (say), the button's size will change too. Thomas's answer achieves this by (essentially) pulling the tab item out of the inheritance tree. My answer does it by giving the button something different to inherit from.
Robert Rossney
Thank you for the explanation, you helped me quite a bit in understanding WPF. I think I worded my question bad. I just wanted the main manu (tabcontrol) to have a completely different look from the controls inside. Then inheritance came in the way. So I tried to find answers regarding "disabling" inheritance which is the wrong way obviously. Thanks again.
Maximilian Csuk
I have found aspects of WPF fantastically difficult to learn precisely because it's impossible to make progress when your mental model of what it's doing is wrong. Most technologies I've learned are more forgiving; if you don't understand something, you can at least get *a* result and see how your ideas fall short. With WPF, stuff either works flawlessly or it doesn't work at all.
Robert Rossney