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:

publicclassBooleanToVisibilityConverter : IValueConverter
{
publicobject Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value asbool? == true) ? Visibility.Visible : Visibility.Collapsed;
}

publicobject ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value isVisibility)
return (Visibility)value == Visibility.Visible;
else
returnfalse;
}
}

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!

Brian Lagunas

View all posts

2 comments

  • Hi Brian.

    Indeed the boolean to visible converter is a must have for any Silverlight, WPF or more to the point, xaml developer.

    I completely agree, Silverlight is being developed. It has a big following (ok not as big as some) but imporant enough for many companies to invest in.

    If you want a rich LOB application you wont go wrong with Silverlight or WPF. I love the framework and am currently involved in a rather big SL project.

    So there is at least one person here who has love for it!

    Finally, thanks to you I have learnt a great deal on xaml and prism development which I cannot thank you enough. I really hope (maybe naively) that MS maintain SL. Have you heard any rumors on SL that would support this now sinofsky is gone?

    • Hey Dan, thanks for the kind words. Now we know of at least two people who still use Silverlight. :0)

      I haven’t heard any rumors, but I am trying to start one. Here is my pitch. With the dismal sales of Windows 8 devices and the lack of adoptance from developers, is Silverlight really dead, or just the backup plan for Windows 8?

      Pretty good huh? Now if only I can get that to spread like wild fire. LOL

Follow Me

Follow me on Twitter, subscribe to my YouTube channel, and watch me stream live on Twitch.