如何在WPF网格中隐藏行?

Mon*_*nes 12 wpf grid

通过将Height属性设置为0,我一直在WPF网格中隐藏一行.

我期待的东西类似于Visible房产.

是否有更合适的方法来隐藏行?

Ian*_*anR 27

您可以将行内容的可见性设置为"Collapsed".这仅在RowDefinition的Height属性设置为"Auto"时才有效,因此行大小基于它的内容.

例如,

<Grid>  
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"  />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>

  <Border Grid.Row="0" BorderThickness="1" BorderBrush="Red"><TextBlock>Visible Row</TextBlock></Border>
  <Border Grid.Row="1" BorderThickness="1" BorderBrush="Black" Visibility="Collapsed"><TextBlock>Hidden Row</TextBlock></Border>
  <Border Grid.Row="2" BorderThickness="1" BorderBrush="Red"><TextBlock>Visible Row</TextBlock></Border>
</Grid>
Run Code Online (Sandbox Code Playgroud)