Skip Navigation LinksHome > View Post

Five minute recipe for a decent BoolToVisibilityConverter

There are a bunch of IValueConverters that WPF probably should ship with... MultiplyConverter, AdditionConverter etc. Rather oddly it does ship with one: The BooleanToVisibilityConverter:

However, I'm not a huge fan of the implementation as it gives me little control over what true and false actually map to. For example, should false be Collapsed or Visible? What if I want to invert? For these reasons I always craft my own (I really must start a codeplex project at some point to keep all these converters). Here it is:

public class BoolToVisibilityConverter : MarkupExtension, IValueConverter
{
    public BoolToVisibilityConverter()
    {
        TrueValue = Visibility.Visible;
        FalseValue = Visibility.Collapsed;
    }

    public Visibility TrueValue { get; set; }
    public Visibility FalseValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool val = System.Convert.ToBoolean(value);
        return val ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return TrueValue.Equals(value) ? true : false;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

As discussed in a previous post, I've used my Converters as MarkupExtensions tip but I've also made the TrueValue and FalseValue completely configurable so there's a clear route for both inversion and choice between Collapsed and Hidden. In fact, you could even have True==Collapsed and False==Hidden if you liked - not sure what you'd use that for though. Note that we set sensible defaults in the constructor.

Here's how to use it:

<StackPanel VerticalAlignment="Bottom">
    <StackPanel.Resources>
        <local:BoolToVisibilityConverter FalseValue="Collapsed" x:Key="btvc" />
    </StackPanel.Resources>
    <CheckBox x:Name="HideOrShowCheck">Hide or show the text...</CheckBox>
    <TextBlock Text="Hello World!" Visibility="{Binding ElementName=HideOrShowCheck, Path=IsChecked,Converter={StaticResource btvc}}" />
</StackPanel>
        
Or, my preferred way:

<StackPanel VerticalAlignment="Bottom">
    <CheckBox x:Name="HideOrShowCheck">Hide or show the text...</CheckBox>
    <TextBlock Text="Hello World!" Visibility="{Binding ElementName=HideOrShowCheck, Path=IsChecked,Converter={local:BoolToVisibilityConverter FalseValue=Collapsed}}" />
</StackPanel>
        
Happy converting.

 
Josh Post By Josh Twist
9:26 AM
12 Oct 2009

» Next Post: Silverlight out-of-browser and initParams
« Previous Post: MultiBinding for Silverlight 3

Comments are closed for this post.

Posted by Simon Draht @ 14 Oct 2009 2:19 PM
Thanks for sharing this, I have experienced the same deficiency, somethimes you want to invert the behaviour, sometimes hide it, sometimes collapse it...
Hence, I also have my own implementation quite similar to yours. Only minor deviation: I additionally added a FallBack-Value, for the case that the bound value is neither true or false.
I built this on a more general bool-to-constant-converter, that allows objects (instead of Visibility-values) as TrueValue, FalseValue and FallbackValue, which covers really lots of different applications.

Posted by Robert @ 06 Sep 2010 4:44 PM
You stated "I really must start a codeplex project at some point to keep all these converters."

Did you ever do this??? It would be extremely valuable.

Posted by Josh @ 06 Sep 2010 6:18 PM
Hmm, sadly not and as much as I would like to, I don't have time. Nothing stopping you, off you go!

I'm serious!

:D

Posted by Robert @ 06 Sep 2010 10:13 PM
I am just an amateur. Wouldn't know where or how to start.

Posted by Dave the Brave @ 01 Nov 2010 9:27 AM
Best Bool To Visibility Converter out there!

Superior Technology ;)

Thanks for sharing!

Posted by K-Walt @ 30 Jan 2011 11:50 PM
Nice job. This is perfect.

© 2005 - 2014 Josh Twist - All Rights Reserved.