gpt4 book ai didi

wpf - 在 WPF 应用程序中将按钮大小设置为内容大小

转载 作者:行者123 更新时间:2023-12-04 00:44:20 27 4
gpt4 key购买 nike

我在 WPF XAML 中有以下代码:

<Window x:Class="WPFDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">

<StackPanel>
<Button Content="Hello World" Width="Auto" Height="Auto"></Button>
</StackPanel>


</Window>

我已将按钮的宽度和高度设置为“自动”,但它仍然会拉伸(stretch)整个水平宽度。我究竟做错了什么?我不想为宽度和高度提供硬编码值!

最佳答案

您使用了错误类型的容器控件。 StackPanel不调整其内容的大小。试试 Grid反而:

<Grid>
<Button Content="Hello" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

此外,您不需要设置 WidthHeight"Auto" ,因为默认值将启用 Button拉伸(stretch)以填充其父容器(取决于容器控件)。请参阅 Panels Overview有关 WPF 中各种容器控件之间差异的更多帮助,请访问 MSDN 页面。

关于wpf - 在 WPF 应用程序中将按钮大小设置为内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24612964/

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