当我隐藏第三个按钮时,我想要第一个按钮和第二个按钮 更高的。我的意思是,当它可见时,他们应该声称被第三个按钮占据的空间。 我怎样才能做到这一点?
我在xaml.cs文件中隐藏了第3个按钮。 Button3.Visibility = Visibility.Visible; 并根据某些条件隐藏它。然后我的代码去了Button3.Visibility = Visibility.Hidden;
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row ="0" x:Name="MyFirstGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Name="Button1" Grid.Column="0" Width="200" Content="Button 1" ></Button>
<Button Name="Button2" Grid.Column="1" Width="200" Content="Button 1" ></Button>
</Grid>
<Grid Grid.Row="1" Height="75" x:Name="MySecondGrid">
<Button Name="Button3" Height="75" Width="200" VerticalAlignment="Center" Content="Button 2" ></Button>
</Grid>
<Button Name="Button4" Grid.Row="2" Height="35" Width="200" Visibility="Visible" Content="Button 3" ></Button>
<Button Name="Button5" Grid.Row="3" Height="35" Width="200" Content="Button 4" ></Button>
</Grid>
答案 0 :(得分:2)
您可以使用DockPanel并将需要额外大小的按钮作为最后一个孩子放置:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="cBoolToVis"/>
</Window.Resources>
<Grid x:Name="grid1">
<DockPanel VerticalAlignment="Top" HorizontalAlignment="Left"
Width="300" Height="180"
KeyboardNavigation.TabNavigation="Local">
<Button DockPanel.Dock="Bottom"
Content="Button 5" Height="35"
KeyboardNavigation.TabIndex="5"/>
<Button DockPanel.Dock="Bottom"
Content="Button 4" Height="35"
KeyboardNavigation.TabIndex="4"/>
<Button DockPanel.Dock="Bottom"
Content="Button 3" Height="75"
Visibility="{Binding ElementName=switch,Path=IsChecked,Converter={StaticResource cBoolToVis}}"
KeyboardNavigation.TabIndex="3"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Content="Button 1"
KeyboardNavigation.TabIndex="1"/>
<Button Grid.Column="1"
Content="Button 2"
KeyboardNavigation.TabIndex="2"/>
</Grid>
</DockPanel>
<ToggleButton x:Name="switch"
Content="Show / Hide"
IsChecked="True"
VerticalAlignment="Bottom" HorizontalAlignment="Left"
Margin="5"/>
</Grid>
点击Show / Hide
按钮进行测试。
您需要设置Visibility=Collapsed
而不是Hidden
,否则隐藏的控件仍会占用其空间。