gpt4 book ai didi

XAML:当我明确说在底部时,为什么按钮在顶部?

转载 作者:行者123 更新时间:2023-12-04 07:10:10 26 4
gpt4 key购买 nike

在 XAML 中,我希望按钮位于红色矩形的底部。

我明确地说:

“水平对齐=”右“
垂直对齐=“底部”

它向右移动,但保持在顶部。这是为什么?

alt text http://tanguay.info/web/external/buttonTop.png

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MultiplePages.Page"
Width="300" Height="150">

<StackPanel HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Width="300" Height="150" Orientation="Vertical">

<StackPanel Width="300" Height="100" Background="Blue">
<TextBlock x:Name="theTextBlock" Text="This is page one. This is page one. This is page one. This is page one. This is page one. This is page one. This is page one. This is page one. This is page one. This is page one. "
TextWrapping="Wrap" Height="100" Width="300" HorizontalAlignment="Left"/>
</StackPanel>

<StackPanel Width="300" Height="50" Background="Red">

<Button Name="Switch" Content="Switch Page" Width="100" Height="20"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"/>

</StackPanel>

</StackPanel>

</UserControl>

最佳答案

StackPanel 旨在从它的元素和容器(取决于它的方向)中调整大小,尽管您所做的看起来是正确的,但这并不是 StackPanel 的“意图”使用方式。尽管它看起来像是它设置的高度,但它的实际高度(用于布置子控件)是其内容(按钮)的大小。 StackPanel 有它的用途,但如果您正在做的不是简单的控件堆栈,那么您通常应该使用其他东西。

您可以通过在内部粘贴一个大小的网格来修复它:

<StackPanel Width="300" Height="50" Background="Red">
<Grid Height="50">
<Button Name="Switch" Content="Switch Page" Width="100" Height="20"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
/>
</Grid>
</StackPanel>

或者,对于该特定布局,您可能需要查看 DockPanel,它的行为更符合您的预期。

关于XAML:当我明确说在底部时,为什么按钮在顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/498961/

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