8

I have a WPF TabControl, with some TabItems. I want margins on the left and right of the TabItems group, if that makes sense.

I'm going to draw some ASCII art below to make the point. I want a fixed margin to the left of tab one, but I also want a fixed margin to the right of tab three.

|--------------------------------------------------|
|            |-----||-----||-----|                 |
| <-Margin-> |  1  ||  2  ||  3  | <-Margin->      |
|------------|     ||-----||-----|-----------------|
|                                                  |
|  How do I get margin or padding on both          |
|    sides of my tabs?                             |
|                                                  |
|                                                  |
|--------------------------------------------------|

The number of tabs is unlimited, so they will stack as more are added. It needs to work correctly for that.

Also, note, that I don't want to make the whole tab control smaller. Just the tabitem tabs or headers or whatever they are.

I found that if I set the tabs to have a margin of something like "60,0,-60,0", I get the desired effect to the left of the tabs, but that seems like a hack, and won't work for the right hand side.

I am using WPF 4.0 in VS 2010.

Cheers!

1 Answer 1

6

Try using this style.

   <Style  TargetType="{x:Type TabControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel
             Grid.Row="0"
             Panel.ZIndex="1"
             Margin="60,0,60,-1"
             IsItemsHost="True"
             Background="Transparent" />
                            <Border
             Grid.Row="1"
             BorderBrush="Black"
             BorderThickness="1"
             CornerRadius="0, 12, 12, 12" >
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Color="LightBlue" Offset="0" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Border.Background>
                                <ContentPresenter ContentSource="SelectedContent" />
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

EDIT

You can give the margin directly to the tabpanel inside the controltemplate of tabcontrol

Check the link for more

http://www.switchonthecode.com/tutorials/the-wpf-tab-control-inside-and-out

1
  • Thank you. That seems to do it. The magic seems to be in that TabPanel. I don't understand it yet, but I believe that's what I want.
    – Michael T
    Mar 23, 2011 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.