2

I am making a WPF application and I have a button that contains "X". By default the font size is 12 and the text is center, but as soon as I increase the font size, the alignment drops. I am unable to understand.

Center text at Font size 12:

Center text at font 12

Off-center text at Font size 24

Off-center text at font 24

My code:

<StackPanel Grid.Row="0" Orientation="Horizontal">
    <Button Content="-" Foreground="White" Margin="756,8,8,0" Height="30" VerticalAlignment="Top" VerticalContentAlignment="Center" BorderThickness="0" Background="{x:Null}" FontWeight="Bold"/>
    <Button Content="X" Foreground="White" Margin="6,8,8,0" Height="30" VerticalAlignment="Top" VerticalContentAlignment="Center" BorderThickness="0" Background="{x:Null}" FontWeight="Bold"/>
</StackPanel>

1 Answers1

4

Putting the content inside a textblock and setting its vertical alignment seems to do what you need:

<Button FontSize="24"
        Margin="756,8,8,0"
        Height="30"
        VerticalAlignment="Top"
        VerticalContentAlignment="Center"
        BorderThickness="2" Background="{x:Null}">
     <Button.Content>
         <TextBlock VerticalAlignment="Center">X</TextBlock>
     </Button.Content>
</Button>
Kay Zed
  • 1,304
  • 2
  • 21
  • 31
Mal
  • 421
  • 3
  • 5