[英]How to make a border around a stackpanel which stops at the last element inside it?
目前我有一个带有 2 个按钮的堆栈面板,我将它们放在网格中的单行中。 我想在 stackpanel 周围画一个边框,但是 stackpanel 填充了整行,它不会停在最后一个按钮上(正如我所料):
我该如何修改它,以便堆栈面板或边框在最后一个元素处停止而无需硬编码或动态计算底部边距?
<Border Background="#FF303841"
Margin="5"
CornerRadius="3"
Grid.Row="2"
Grid.Column="4">
<StackPanel>
<TextBlock Text="OTHER"
Foreground="White"
FontWeight="Medium"
FontFamily="Poppins"
FontSize="16"
HorizontalAlignment="Left"
Margin="5,5,0,0"/>
<Button Style="{StaticResource linkBtn}">TEST1</Button>
<Button Style="{StaticResource linkBtn}">TEST2</Button>
</StackPanel>
</Border>
根据上面 emoacht 的评论,解决方案是简单地将 VerticalAlignment="Top" 添加到边框。 这使它停止填充作为网格行的父级。
出现这种行为的原因是 VerticalAlignment 的默认参数是stretch,所以需要重写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.