Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretching not applying in a WPF templated Button

I created a templated button in WPF :

<ControlTemplate TargetType="{x:Type Button}"
                 x:Key="BoutonGris">
  <Button  Foreground="White"
           BorderBrush="Transparent"
           HorizontalAlignment="Center"
           VerticalAlignment="Center"
           HorizontalContentAlignment="Stretch"
           VerticalContentAlignment="Stretch">
    <Button.Content>
      <Border CornerRadius="3"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch">
        <ContentPresenter />
        <Border.Style>
          <Style TargetType="{x:Type Border}">
            <Setter Property="Background"
                    Value="#58585a" />
            <Style.Triggers>
              <Trigger Property="IsMouseOver"
                       Value="True">
                <Setter Property="Background"
                        Value="{DynamicResource DegradeCouleurTheme}" />
              </Trigger>
            </Style.Triggers>
          </Style>
        </Border.Style>
      </Border>
    </Button.Content>
    <Button.Style>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
              <ContentPresenter />
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Button.Style>
  </Button>
</ControlTemplate>

I use this button in my Window :

<Button x:Name="BtnRestoreDefault" Template="{DynamicResource BoutonGris}" Content="Rest. défaut" Height="27"  Width="88" Click="Button_Click_RestoreDefault"/>

My problem is that the button don't appear to have the Height="27" and Width="88".

Here is my result in VS2012 :

The button has the good size, but the gray area don't. I have use the Stretch keywords on the Template but i don't find the mistake.

If i use Stretch for HorizontalAlignment, HorizontalContentAlignment, VerticalAlignment, and VerticalContentAlignment, the gray has the good size, but, the text content is in left/up ! I would like a center text.

Anyone have an idea please ?

Thanks a lot,

Best regards,

Nixeus

like image 736
Walter Fabio Simoni Avatar asked Dec 26 '22 05:12

Walter Fabio Simoni


1 Answers

This should do the trick.

<ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonGris">
    <Button  Foreground="White" BorderBrush="Transparent" 
             HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
             HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >
        <Button.Content>
            <!-- A lot of your code -->
        </Button.Content>
        <!-- A lot of your code -->
    </Button>
</ControlTemplate>

One question, why do you have a Button in your ControlTemplate for a Button? Usually, I would expect simple controls like Border etc here, because you want to reimplement the visual appearance.

Edit

One of the ContentPresenter should be declared like this.

<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>

I'm not sure which one. But you can try both. ;o)

like image 127
DHN Avatar answered Dec 28 '22 23:12

DHN