我在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>
Run Code Online (Sandbox Code Playgroud)
我已将Button的宽度和高度设置为"Auto",但仍然会拉伸整个水平宽度.我究竟做错了什么?我不想为宽度和高度提供硬编码值!
She*_*dan 17
您正在使用错误的容器控件.该StackPanel
不会不调整其内容.尝试Grid
改为:
<Grid>
<Button Content="Hello" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
此外,您不需要设置Width
和Height
到"Auto"
,作为默认值将使Button
伸展,以填补它的父容器(依赖于容器控件).有关WPF中各种容器控件之间差异的更多帮助,请参阅MSDN上的" 面板概述"页面.
正如谢里丹所说,您不需要将宽度和高度设置为“自动”。您的问题是 StackPanel 内容的默认对齐方式是“Stretch”。因此,只需将按钮的 HorizontalAlignment 属性设置为“左”或“右”。
归档时间: |
|
查看次数: |
16745 次 |
最近记录: |