user2088807
user2088807

Reputation: 1408

Force my tabitem to calcultate its width

I have the following style for my tabitems :

<Style x:Key="BoutonMenuHaut" TargetType="{x:Type TabItem}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Border Height="40" HorizontalAlignment="Stretch" CornerRadius="0" Name="border">
                            <Grid>
                                <ContentPresenterVerticalAlignment="Center"
                          HorizontalAlignment="Left"
                            TextBlock.Foreground="{StaticResource CouleurTexteBoutonsHaut}"
                            TextBlock.FontSize="{DynamicResource FontSizeBig}"
                            Name="textTab" 
                            ContentSource="Header" Margin="10,0,60,0"
                            RecognizesAccessKey="True">
                                </ContentPresenter>
                                <Rectangle Height="2" x:Name="separation" Fill="Red" VerticalAlignment="Bottom" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

As you can see I bind the fontsize to a value defined like this :

xmlns:sys="clr-namespace:System;assembly=mscorlib"
<sys:Double x:Key="FontSizeBig">16</sys:Double>

And in the window_sizechanged event I do this :

this.Resources["FontSizeBig"] = TextSizeFromResolution(12, height);

where height is the height of the screen.

But when I resize my window my tabitem doesn't resize itself and i'm not able to see all my tabs in the page anymore.

So my question is : How can I force the tabcontrol to resize the tabitems when the size changes ? (because the size of the text has changed so the tabitem's width should change too. I've tried InvalidateMeasure, Update Layout... but I could not manage to make it work.

Could you help me ?

Thanks !

Upvotes: 0

Views: 113

Answers (1)

Henriette van de Haar
Henriette van de Haar

Reputation: 120

You could bind the width of the tabs to the ActualWidth of the tab control with a converter:

          <Style TargetType="TabItem">
          <Setter Property="Width" Value="{Binding    
                  Path=ActualWidth,  Converter={StaticResource YourConverter},  
                 RelativeSource={RelativeSource   
                FindAncestor, AncestorType={x:Type TabControl}}}"/>
          </Style>

Upvotes: 1

Related Questions