logeeks
logeeks

Reputation: 4979

automatic alignment of buttons

I got 4 buttons aligned inside a grid. Is there any way to make alignment automatic so that if one button's visibility is set to false the remaining buttons uses the space wisely[other buttons moves to accommodate into new space]?

Upvotes: 3

Views: 725

Answers (3)

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

Try the following:

<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
    <StackPanel Orientation="Horizontal">
        <Button Content="1" Width="50" Height="23" Visibility="Collapsed"/>
        <Button Content="2" Width="50" Height="23"/>
        <Button Content="3" Width="50" Height="23" Visibility="Collapsed"/>
        <Button Content="4" Width="50" Height="23"/>
    </StackPanel>
</Grid>

Or this:

<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Button Grid.Column="1" Content="1" Width="50" Height="23" Visibility="Collapsed"/>
    <Button Grid.Column="2" Content="2" Width="50" Height="23"/>
    <Button Grid.Column="3" Content="3" Width="50" Height="23" Visibility="Collapsed"/>
    <Button Grid.Column="4" Content="4" Width="50" Height="23"/>
</Grid>

You should see the buttons centered but only show the second and fourth button

Upvotes: 2

brunnerh
brunnerh

Reputation: 184506

Depends on the layout and how complex you want the space distribution to be, if a uniform distribution is enough you could use a UniformGrid, in a normal Grid that would be a bit tricky as the columns and rows need to be adjusted.

Upvotes: 1

Guy Starbuck
Guy Starbuck

Reputation: 21873

Instead of a grid, put the buttons in a StackPanel with Orientation="Horizontal". Also, make sure you set your button Visibility to "Collapsed" instead of "Hidden".

Upvotes: 2

Related Questions