【问题标题】:How do I align button text to center in WPF C#?如何在 WPF C# 中将按钮文本对齐到中心?
【发布时间】:2019-05-21 06:45:53
【问题描述】:

我正在创建一个WPF 应用程序,并且我有一个包含"X"button。默认情况下,字体大小为 12,文本居中,但是一旦我增加字体大小,对齐就会下降。我无法理解。

以 12 号字体居中文本:

字体大小为 24 的偏离中心文本

我的代码:

<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>

【问题讨论】:

    标签: c# .net wpf button


    【解决方案1】:

    将内容放在文本块中并设置其垂直对齐似乎可以满足您的需要:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2015-05-16
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多