StackPanel 中的按钮

Buttons inside StackPanel

我有堆栈面板(1 行 x 6 列)包含 6 个按钮:

 <StackPanel Background="AntiqueWhite" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="6" Orientation="Horizontal">
            <Button x:Name="a1" Content="a1" />
            <Button x:Name="a2" Content="a2" />
            <Button x:Name="a3" Content="a3" />
            <Button x:Name="a4" Content="a4" />
            <Button x:Name="a5" Content="a5" />
            <Button x:Name="a6" Content="a6" />
        </StackPanel>

现在它们如下所示:

如何设置,一键成一栏?

更新:网格定义:

<Grid Background="AliceBlue">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

我认为你需要允许每个按钮在一列中,例如

<Button x:Name="a1" Content="a1" Grid.Column="0" />
<Button x:Name="a2" Content="a2" Grid.Column="1" />

因此,为此在 Stackpanel 中定义一个网格,例如:

<StackPanel>
<Grid>
     <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
        ....
    </Grid.ColumnDefinitions>
</Grid>
<Buttons Grid.Column="..."/>
</StackPanel>

像这样:

<Grid Background="AliceBlue">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/> //width will be divided equally among 6 columns
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            //Row definations will go here
        </Grid.RowDefinitions>
        <Button Content="a1" Grid.Column="0" />
        <Button Content="a2" Grid.Column="1" />
        <Button Content="a1" Grid.Column="2" />
        <Button Content="a2" Grid.Column="3" />
        <Button Content="a1" Grid.Column="4" />
        <Button Content="a2" Grid.Column="5" />
    </Grid>