【发布时间】:2019-04-14 07:46:31
【问题描述】:
在以下 XAML 中,按钮将拉伸以适应窗口的宽度,包括在您调整窗口大小时。但是,TextBlock 和蓝色框居中。你会如何改变它,以便: 1) TextBlock 位于 Button 内部,但与实际 Button 宽度左对齐(即在 Window 的左侧) 2) Canvas 在 Button 内部,但与实际 Button 宽度右对齐(即在 Window 的右侧)
在这种情况下,“HorizontalAlignment=Stretch”似乎不起作用,并且在使用自动调整大小时,Button 内的 Grid 只会增长到其内容所需的最小宽度。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="test"
Width="640" Height="480">
<Grid x:Name="LayoutRoot">
<Button Height="30">
<Grid HorizontalAlignment="Stretch">
<TextBlock Text="Sample Text" HorizontalAlignment="Stretch" TextAlignment="Left"></TextBlock>
<Canvas Width="40" Background="AliceBlue" HorizontalAlignment="Right"></Canvas>
</Grid>
</Button>
</Grid>
</Window>
【问题讨论】: