Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide grid row in WPF

Tags:

wpf

grid

I have a simple WPF form with a Grid declared on the form. This Grid has a bunch of rows:

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

The row named rowToHide contains a few input fields and I want to hide this row after I detect I don't need these fields. It's simple enough to just set Visibility = Hidden to all items in the row, but the row still takes up space in the Grid. I tried setting Height = 0 to the items, but that didn't seem to work.

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

like image 426
Richard Avatar asked Mar 23 '10 17:03

Richard


People also ask

How to hide Grid row in wpf?

Simply do this: rowToHide. Height = new GridLength(0);


1 Answers

Row does not have a Visibility property, so as others have said, you need to set the Height. Another option is to use a converter, in case you need this functionality in many views:

    [ValueConversion(typeof(bool), typeof(GridLength))]     public class BoolToGridRowHeightConverter : IValueConverter     {         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)         {             return ((bool)value == true) ? new GridLength(1, GridUnitType.Star) : new GridLength(0);         }          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)         {    // Don't need any convert back             return null;         }     } 

And then in the appropriate view <Grid.RowDefinition>:

<RowDefinition Height="{Binding IsHiddenRow, Converter={StaticResource BoolToGridRowHeightConverter}}"></RowDefinition> 
like image 57
testpattern Avatar answered Oct 24 '22 13:10

testpattern