How to equally divide Stackpanel’s space between children

Asked

Viewed 109 times

1

I have a Stack Panel with a few buttons inside, I want the available Stack space to be divided equally between the buttons.

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Content="USUÁRIOS" Height="auto"/>
            <Button Content="IM" Height="auto" />
            <Button Content="COMPOSIÇÃO" Height="auto" />
            <Button Content="DEFEITOS" Height="auto" />
            <Button Content="PERDAS" Height="auto" />
            <Button Content="DIÁRIO" Height="auto" />
            <Button Content="GERENCIAR" Height="auto" />
            <Button Content="GED" Height="auto"/>
        </StackPanel>

The output is as follows: Captura

I want the buttons to fill all that blank space with equal sizes.

1 answer

2


The stackpanel would not be very usual in this case, I recommend you use a grid and set the columns of it distributing the buttons in each of the columns.

<Grid Height="35">

    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Button Content="USUÁRIOS" Height="auto" Grid.Column="0"/>
    <Button Content="IM" Height="auto"  Grid.Column="1"/>
    <Button Content="COMPOSIÇÃO" Height="auto" Grid.Column="2"/>
    <Button Content="DEFEITOS" Height="auto" Grid.Column="3"/>
    <Button Content="PERDAS" Height="auto" Grid.Column="4"/>
    <Button Content="DIÁRIO" Height="auto" Grid.Column="5"/>
    <Button Content="GERENCIAR" Height="auto" Grid.Column="6"/>
    <Button Content="GED" Height="auto" Grid.Column="7"/>
</Grid>

Where "Grid.Columnsdefinition" divides the grid into the amount of "Columndefinition" you insert. In the buttons you only define each one in its respective column. By increasing or decreasing the screen at runtime, your buttons will continue to be adjusted evenly.

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.