Thursday, May 7, 2009

WPF: boolean to visibility converter

Hi Folks,

recently I had the pleasure to remove home-brewn converters named VisibilityOfBoolean and VisibilityOfChecked and started using System.Windows.Controls.BooleanToVisibilityConverter instead.

Example of usage in XAML, experienced reader fills in the details:

<BooleanToVisibilityConverter key="VisibilityOfBool" />

and later:

Visibility="{Binding ElementName=checkboxVisible, Path=IsChecked, Converter={StaticResource VisibilityOfBool}}"

Easy as a pie. I only wish there was an equally easy way to invert a boolean value!

11 comments:

Michael said...

I thought Wow, cool, this is great!! Now I can't seem to find it. What reference did you have to add? I used the object browser to see if I could find it, went through several references?

Michael said...

Ignore, I keep forgetting that Silverlight only seems like it is WPF...

Ciantic said...

We should make some noise on these weak parts of WPF framework.

I opened suggestion about WPF Boolean inversion converter to Microsoft Connect bug/suggestion system.

If you care about this enhancement please vote on it.

lucho said...

// Converter that negates a boolean value
[ValueConversion(typeof(bool), typeof(bool))]
public class NotConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ConvertValue(value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return ConvertValue(value);
}
private static object ConvertValue(object value)
{
if (!(value is bool))
{
throw new NotSupportedException("Only bool is supported.");
}
return !(bool)value;
}
}

Tim said...

Whoa, that really is a winner.

Tim said...

But does it default to collapse or hidden?

Nathan said...

[ValueConversion(typeof(bool), typeof(bool))]
public class BooleanNotConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!targetType.IsAssignableFrom(typeof(bool)))
throw new ArgumentException("targetType must be bool");
if (!(value is bool))
throw new NotSupportedException("value must be a bool");
return !(bool)value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture);
}
}

Visibility said...

A lot of times, I need to show or hide a textbox based the value of a checkbox. In WinForms 2.0. This was easy:
myTextBox.Visible = myCheckBox.Checked;

With the new Visibility Enum in WPF, this becomes a bit trickier. To accomplish this, you need to implement a converter that will accept a boolean value and return a visibility value.

GertD said...

This example uses Visibility and not bool:

[System.Windows.Data.ValueConversion(typeof(Synergy.Objects.BusinessEntity), typeof(System.Windows.Visibility))]
public class BusinessEntityRatingClient : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
Synergy.Objects.BusinessEntity be = (Synergy.Objects.BusinessEntity)value;

// Need business clients to get this rating
return (be.BusinessClients != null && be.BusinessClients.Count > 0) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("Not needed");
}
}

Rhyous said...

I wrote a post that extends this post with more information.

Binding Visibility to a bool value in WPF

Hope it helps out readers who need a few more examples and need to do a little more customization.

Mykola Rykov said...

Thank you!
I've used it in my project.
This approach is really better for a simple situations!