tags:

views:

1168

answers:

3
+3  Q: 

WPF: Hide grid row

Hi All,

I have a simple WPF form with a <Grid> declared on the Form.

In this Grid I then have a bunch of Rows:

   <Grid.RowDefinitions>
        <RowDefinition Height="Auto" MinHeight="30" />
        <RowDefinition Height="Auto" Name="rowToHide"/>
        <RowDefinition Height="Auto" MinHeight="30" />
    </Grid.RowDefinitions>

So basically the row with the name "rowToHide" has a few input fields in it, and now I want to hide this row as I don't need these fields. Its simple enough to just set all items in the Row to Visibility = Hidden, but the Row still takes up space in the Grid. So need to do something like setting Height = 0 or something. But that didn't seem to work.

You can think of it like this: You have a form, in there we have a drop down saying "Payment Type", and if the person selects "Cash", then hide the row containing the Card details. And it isn't an option to start the form with this hidden already.

Thanks everyone!

+1  A: 

Set the Row's content visibility to Visibility.Collapsed instead of Hidden. This will make the content stop taking up space, and the row will shrink appropriately.

Reed Copsey
I've seen somewhere else someone mentioned the Row Visibility. But the Row doesn't have a visibility state? Setting all the Items in the Row to Visibility.Collapsed worked though.
Richard
@Richard: You can't set RowDefinition.Visibility since it's not a UIElement - but you can put all of your content for the row (or each column within the row) into a single container, and set that container's visibilty.
Reed Copsey
+1  A: 

For reference, Visibility is a three-state System.Windows.Visibility enumeration:

  • Visible - The element gets rendered and participates in layout.
  • Collapsed - The element is invisible and does not participate in layout. Effectively giving it a height and width of 0 and behaving as if it doesn't exist.
  • Hidden - The element is invisible but continues to participate in layout.

See this tip and other tips on the WPF Tips and Tricks thread.

Metro Smurf
Setting all the Items in the Row to Visibility.Collapsed worked, thanks.
Richard
A: 

You can also do this by referencing the Row in the Grid and then changing the Height of the row itself.

XAML

<Grid Grid.Column="2" Grid.Row="1" x:Name="Links">
   <Grid.RowDefinitions>
      <RowDefinition Height="60" />
      <RowDefinition Height="*" />
      <RowDefinition Height="*" />
      <RowDefinition Height="80" />
   </Grid.RowDefinitions>
</Grid>

VB.NET

If LinksList.Items.Count > 0 Then
   Links.RowDefinitions(2).Height = New GridLength(1, GridUnitType.Star)
Else
   Links.RowDefinitions(2).Height = New GridLength(0)
End If

Whilst the Collapsing of the elements within the Grid also works, this is a bit simpler if you have many items in the Grid that does not have an enclosing element that can be collapsed. This would provide a good alternative.

TravisPUK