2012-01-17 122 views
3

我有4個按鈕在網格內對齊。有沒有什麼方法可以自動對齊,如果一個按鈕的可見性設置爲false,其餘的按鈕會明智地使用該空間[其他按鈕移動以適應新的空間]?按鈕自動對齊

回答

2

而不是一個網格,將按鈕放在一個方向=「水平」的StackPanel。另外,請確保將按鈕可見性設置爲「摺疊」而不是「隱藏」。

1

取決於佈局以及您希望空間分佈的複雜程度,如果均勻分佈足夠您可以使用UniformGrid,在正常的Grid中,由於需要調整列和行會有點棘手。

2

嘗試以下操作:

<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> 

或者這樣:

<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> 

你應該看到的按鈕爲中心,但只顯示了第二個和第四個按鈕