WPF Hidden和Collapsed
对于这两种设定,其实已经提示的很清楚了
Visibility="Hidden"
不显示元素,但是在布局为元素保留空间
Visibility="Collapsed"
不显示元素,并且不在布局中为它保留空间
<Grid ShowGridLines="True"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"></ColumnDefinition> <ColumnDefinition Width="auto"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <Button Width="100" Height="100" Grid.Column="0" Grid.Row="0" Visibility="Hidden"/> <Button Width="100" Height="100" Grid.Column="0" Grid.Row="1" /> </Grid>
可以看见,空白区域依旧保留位置
<Grid ShowGridLines="True"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"></ColumnDefinition> <ColumnDefinition Width="auto"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <Button Width="100" Height="100" Grid.Column="0" Grid.Row="0" Visibility="Collapsed" /> <Button Width="100" Height="100" Grid.Column="0" Grid.Row="1" /> </Grid>
已经不保留其位置