是否可以有一个 <StackPanel>
或标签(或按钮等)水平穿过它的父控件(例如 Window
)并填充所有允许的空间?
例如,如果我有 3 个控件
_ window width__
[ 1 ]__ [ 2 ] __ [ 3 ]
或 2 个控件
_ window width__
[ 1 ]_______ [ 2 ]
在每种情况下,窗口的整个宽度都被利用,每个边缘控件分别左对齐和右对齐。
最佳答案
StackPanel 将堆叠控件,所以不是简单的答案。这不是 StackPanel 的设计目的。
您可以按照 Vlad 的建议使用网格。像这样的东西:
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Button Width="20" Height="20" Grid.Column="0"/>
<Button Width="20" Height="20" Grid.Column="2"/>
<Button Width="20" Height="20" Grid.Column="4"/>
</Grid>
关于wpf - 水平 Stackpanel 填充父控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9912072/