I have been hiding a row in a WPF grid by setting the Height
property to 0.
I was expecting something akin to a Visible
property.
Is there a more appropriate way to hide the row?
I have been hiding a row in a WPF grid by setting the Height
property to 0.
I was expecting something akin to a Visible
property.
Is there a more appropriate way to hide the row?
You could set the visibility of the row's content to "Collapsed". This will only work if the Height property of the RowDefinition is set to "Auto" so the row sizes based on it's content.
For example,
<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>
I actually just asked the same question a couple of days ago, take a look here:
Basically setting the RowHeight to Auto and then Setting the Visibility="Collapsed" will hide the row for you. The only issue I had was the Margins, but that was minor. At least the row got hidden.
Just do this :
XAML :
<Grid.RowDefinitions>
<RowDefinition Height="1*" x:Name="name1" />
<RowDefinition Height="Auto" x:Name="name2" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
C# for collapse :
name1.Height = new GridLength(0);
name2.Height = new GridLength(0);
C# for visibility:
name1.Height = new GridLength(1, GridUnitType.Star);
name2.Height = GridLength.Auto;