A Silverlight BooleanToVisibilityConverter

Here is my Silverlight BooleanToVisibilityConverter.  I know this is a very common and simple converter that every Silverlight developer has in their toolbox.  It’s not a new concept or groundbreaking tip, and a quick Google/Bing search will find you many flavors of its’ implementation.  So why post it now?  Especially given the current state of Silverlight?  Well, as you all know, Silverlight isn’t getting much love (no love really) these days, but Silverlight development is still happening. 

What did you say Brian?  Yes, believe it or not, Silverlight code is still being written (GASP!).  So I figured I will show Silverlight a little love, and share a little piece of my code base with the world.  I am simply sharing an implementation of a BooleanToVisibilityConverter that I use in my Silverlight applications.  It’s nothing fancy, and I am probably posting this more for me than the community.  I use this so often, I need an easily accessible place to keep it so that it is easy to find.  No more opening past projects and solutions and digging through a code base to get it.  A simple search on my blog will bring it to my fingertips.

Here’s the code: (UPDATED: based on the feedback in the comments section, I have updated the implementation)

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (value as bool? == true) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Visibility)
            return (Visibility)value == Visibility.Visible;
        else
            return false;
    }
}

Usage:

<Grid.Resources>
    <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
Grid.Resources>

<ToggleButton Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"/>

Note:  The “converters” namespace has been declared at the top of my view and points to the location of the converter in my application.

Maybe this simple converter can be useful for you.  If not, then hopefully you didn’t waste your time by reading the entire post.

I am still amazed that this simple converter never made it into the Silverlight code base.  Go figure!


Comments  (5 )

mstrobel
on Thu, Jan 31 2013 8:47 PM

The check for Nullable<bool> is unnecessary; 'value' cannot possibly be a Nullable<bool> because there is no such thing as a boxed nullable value type.  Nullable value types are boxed as their underlying value or as null.  You can, however, use the 'is' and 'as' operators to check or unbox value types to their nullable counterparts.  In other words, your entire Convert method could be reduced as:

return (value as bool? == true) ? Visibility.Visible : Visibility.Collapsed;

Brian Lagunas
on Thu, Jan 31 2013 11:07 PM

Thanks, that's a great approach.  I love seeing other ways of doing things.  Personally, I like the verbosity of my approach.  It's probably because I have been using that snippet since the early days of Silverlight, and it has just grown on me.  That is actually the WPF implementation that I just copied over to Silverlight back in the day.  I'm a creature of habit.  I am sure the community will appreciate your modification to this post.  Keep the feedback coming.

mstrobel
on Fri, Feb 1 2013 5:28 AM

Indeed, I always thought it was strange that the Microsoft implementation worked that way, as the entire 'else if' body is effectively dead code (it cannot ever execute).  The only time you might need to consider nullable value types during type conversion is when checking the target type (for converters capable of converting to/from multiple types).  The boxed value passed to Convert(Back) cannot be a nullable value type, but the target type certainly can be.

Here's another useful tidbit about C#: when the null coalescing operator '??' is used with a nullable value type on the left hand side and a non-nullable value type on the right hand side, the resulting expression is non-nullable.  This is useful for falling back to a default value.  For example, given a boxed object 'obj', the statement "int x = (obj as int?) ?? 42" will result in 'x' being either an unboxed integer or 42 (if 'obj' was null or anything other than an integer).

Brian Lagunas
on Fri, Feb 1 2013 10:18 AM

You have convinced me to change the implementation I have been using for all these years to your suggestion.  You have also inspired me to start a new blog series.  First post will be coming soon.

Jason Beres
on Fri, Feb 1 2013 12:59 PM

Awesome!  We love Silverlight and especially Silverlight blog series!  Thanks Brian & mstrobel!

Add a Comment

Please Login or Register to add a comment.