水平Stackpanel填充父控制

Ter*_*ary 12 wpf layout xaml stackpanel

是否可以在<StackPanel>其父控件(例如Window)上水平放置一个或标签(或按钮,等等),并填写所有允许的空间?

例如,如果我有3个控件

_ window width_ _

[1] _ _ [2] _ _ [3]

或2个控件

_ window width_ _

[1] _ __ _ __ _ [2]

在每种情况下,利用窗口的整个宽度,每个边缘控制分别左右对齐.

Mat*_*and 22

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>
Run Code Online (Sandbox Code Playgroud)