【问题标题】:Text is not centered in modified button文本未在修改按钮中居中
【发布时间】:2020-04-16 20:46:12
【问题描述】:

以前的解决方案似乎都不起作用。我有一些经过大量修改的按钮,如下所示:

而且,如您所见,按钮中的文本太高了。它需要降低。更改边距不起作用。找不到任何可以调整类型出现在按钮中的位置。这是 XAML:

     <Button Grid.Row="0" x:Name="LaunchArmyEditor"  Content="Run the General Staff Army Editor" Height="86" Width="600" Click="LaunchArmyEditor_Click" FontFamily="Amaltea WF" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,0,0" BorderBrush="{x:Null}" FontWeight="Bold"  Cursor="Hand" Margin="0,-1,0,0"  >
                    <Button.Background>
                        <ImageBrush ImageSource="TransVictorianButton.png" Stretch="None"/>
                    </Button.Background>
                </Button>

                <Button Grid.Row="1" x:Name="LaunchMapEditor"   Content="Run the General Staff Map Editor" Height="86" Width="600" Click="LaunchMapEditor_Click" FontFamily="Amaltea WF" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="0,0,0,0" BorderBrush="{x:Null}" FontWeight="Bold"  Cursor="Hand" >
                    <Button.Background>
                        <ImageBrush ImageSource="TransVictorianButton.png" Stretch="None"/>
                    </Button.Background>
                </Button>

有什么建议吗?

【问题讨论】:

  • 您是否尝试设置Padding 属性?
  • 好的,谢谢。它在 Layout 下,而不是 Text,所以我没有想到。
  • 您应该使用可重复使用的样式和模板,而不是一遍又一遍地使用几乎相同的标记。问题可能是字体。那些 walden 字体不能正确测量。至于如何解决这个问题。当您将按钮(或标签)的内容设置为字符串时,控件中有代码在控件中插入文本块并将其文本绑定到内容属性。因此,您有一个文本块是按钮的内容,您可以使用按钮的资源对其应用样式。

标签: wpf xaml button text fonts


【解决方案1】:

使按钮的内容成为标签/文本框,而不是属性。然后相应地调整正在使用的控件,例如使用Margin属性,如下所示:

<Button … >
    <ContentControl>
        <Label Content="Run the General Staff Map Editor" Margin="0,4,0,0"/>
    </ContentControl>
    <Button.Background>
        <ImageBrush ImageSource="TransVictorianButton.png" Stretch="None"/>
    </Button.Background>
</Button>

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 2022-08-18
    • 2012-07-28
    • 2020-10-20
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多