tags:

views:

234

answers:

1

Hi,

I have a grid that has multiple rows. I want to hide/show one of those rows based on a property. Is that possible?

In my case, I have two grid rows. One has a property grid and the other a list box. The list box is bound to an object and the list items are bound to an array inside that object. What I want is to hide the row (including the list box) whenever the array is empty. So when my object's array is empty, you should just see a window with a property grid and nothing else.

Thanks!

+2  A: 

Yes. Bind the ListBox's Visibility property to the object that contains the array. Then apply a custom value converter that will look at the array and see if it is empty. If it is empty, return Visibility.Collapsed. Otherwise, return Visibility.Visible. Then make sure your RowDefinition has a height of Auto, and it will automatically shrink to nothing when the ListBox is collapsed.

Your value converter will look something like this:

public class EmptyVisiblityConverter : IValueConverter
{
 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
  YourObject yourObject = value as YourObject;
  return yourObject.YourArray.Count > 0 ? Visibility.Visible : Visibility.Hidden;
 }

 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
  return null;
 }
}

And your XAML should look something like this:

<Window.Resources>
    <local:EmptyVisiblityConverter x:Key="emptyVisibilityConverter"/>
</Window.Resources>
...
<ListBox Visibility="{Binding Path=YourObject, Converter={StaticResource emptyVisibilityConverter}}"/>
Charlie
This solution works for me!
Steve the Plant