gpt4 book ai didi

c# - 在 Windows 8.1 XAML/C# 应用程序中,如何精确设置按钮样式?

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:09 24 4
gpt4 key购买 nike

我是 Windows 8.1 开发的新手,如果这个问题的答案很明显,请原谅我,但我已经为此苦苦挣扎了一段时间,但无法让它发挥作用。情况是这样的:

我制作了一个 UserControl 以便为按钮设置样式并向其添加其他功能。这是相关代码:

<UserControl
x:Class="MyCalculator.CalculatorButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyCalculator"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d"
>
<!-- THIS GRID TAKES UP THE WHOLE WIDTH AND HEIGHT -->
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- THIS BUTTON TAKES UP THE WHOLE SIZE OF THE GRID -->
<Button
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
BorderBrush="Blue"
BorderThickness="1"
Background="AliceBlue"
Padding="0"
Content="How can I make this bigger?"
Foreground="Blue"
Grid.Row="0"
Grid.Column="0"
/>
</Grid>
</UserControl>

这是它在设计器中的样子: The Grid takes up the whole space, and the Button takes up the whole space, but the Button's color and border are clearly much smaller

您会注意到 Button 看起来在水平和垂直方向上都居中,而且很小。但是,当我单击代码中的 Button 时,Grid 的整个尺寸(在设计器中占据了整个 400 x 300 区域)都被 Button 填充了。

所以 Button 正在填充 Grid,而 Grid 正在填充该区域。但是Button的Border和Background超小,在Button中间。更糟糕的是,它甚至不会拉伸(stretch)以包含文本。这显然不是所需的外观和感觉。

试图让它做什么?我想让 AliceBlue 按钮背景填满整个 Grid,填满整个 400 x 300 区域。我想要“我怎样才能让它变大?” blue 内容字符串在将导致的大范围 AliceBlue 中垂直和水平居中。

我在网格和按钮上尝试了无数种规范组合,包括边距、填充、高度、宽度和其他属性。没有任何东西影响按钮的 AliceBlue 区域的大小或位置。

显然我遗漏了什么。任何人都可以帮助我理解那是什么,以及如何格式化我的按钮(或一般的 UserControl)以使其看起来像我想要的那样?

预先感谢您的帮助!

最佳答案

您是否尝试放入 TextBlock 子元素,而不是将字符串值分配给 Content 属性?这可能会给你更多的控制嗨。 Button 具有您可以设置的 Horizo​​ntalContentAlignment 和 VerticalContentAlignment。然后就可以独立修改按钮内部子TextBlock的Horizo​​ntalAlignment和VerticalAlignment。

关于c# - 在 Windows 8.1 XAML/C# 应用程序中,如何精确设置按钮样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29811723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com