Geeks With Blogs

@codingbloke
  • codingbloke Blogged: Using the AsyncOperationService with HttpWebRequest. Builds on the previous .NET Async Pattern stuff. http://t.co/B5VLg8a about 1101 days ago
  • codingbloke I haven't blogged in a while but http://t.co/jzEgzME its more on AsyncOperationService. Future posts will show more practical uses. about 1143 days ago
  • codingbloke @Pete_Brown I like "custom" mode, gives me the warm fuzzy feel that I'm in control about 1319 days ago

News
The Coding Bloke An ordinary bloke trying to keep up with the world of programming

On fairly regular intervals a question on Stackoverflow like this one:  Silverlight Bind to inverse of boolean property value appears.  The same answers also regularly appear.  They all involve an implementation of IValueConverter and basically include the same boilerplate code.

The required output type sometimes varies, other examples that have passed by are Boolean to Brush and Boolean to String conversions.  Yet the code remains pretty much the same.  There is therefore a good case to create a generic Boolean to value converter to contain this common code and then just specialise it for use in Xaml. Here is the basic converter:-

BoolToValueConverter
using System;
using System.Windows.Data;

namespace SilverlightApplication1
{
    public class BoolToValueConverter<T> : IValueConverter
    {
        public T FalseValue { get; set; }
        public T TrueValue { get; set; }

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

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

With this generic converter in place it easy to create a set of converters for various types.  For example here are all the converters mentioned so far:-

Value Converters
using System;
using System.Windows;
using System.Windows.Media;

namespace SilverlightApplication1
{
    public class BoolToStringConverter : BoolToValueConverter<String> { }
    public class BoolToBrushConverter : BoolToValueConverter<Brush> { }
    public class BoolToVisibilityConverter : BoolToValueConverter<Visibility> { }
    public class BoolToObjectConverter : BoolToValueConverter<Object> { }
}

With the specialised converters created they can be specified in a Resources property on a user control like this:-

<local:BoolToBrushConverter x:Key="Highlighter" FalseValue="Transparent" TrueValue="Yellow" />
<local:BoolToStringConverter x:Key="CYesNo" FalseValue="No" TrueValue="Yes" />
<local:BoolToVisibilityConverter x:Key="InverseVisibility" TrueValue="Collapsed" FalseValue="Visible" />
Posted on Friday, May 28, 2010 1:07 PM Silverlight | Back to top


Comments on this post: A Generic Boolean Value Converter

# re: A Generic Boolean Value Converter
Requesting Gravatar...
Very nice & usefull!
Thanks, keep up the good work :-)
Left by Ronald on May 10, 2011 7:56 AM

# re: A Generic Boolean Value Converter
Requesting Gravatar...
Good one. Will implement this one right away.
Thanks.
Left by Vijay on Apr 06, 2012 4:39 AM

# re: A Generic Boolean Value Converter
Requesting Gravatar...
It's also nice to add the following constructor, so you can create a converter in the variable declaration instead of the window app constructor:

public BoolToValueConverter(T falseVal, T trueVal)
{
FalseValue = falseVal;
TrueValue = trueVal;
}
Left by Jim on Apr 11, 2012 5:21 PM

# re: A Generic Boolean Value Converter
Requesting Gravatar...
Very useful. Thank you!
Left by Frieder on Jan 04, 2013 5:03 AM

# re: A Generic Boolean Value Converter
Requesting Gravatar...
I would have expected only TrueValue and FalseValue to return "true" or "false", and vice-versa.

So in Convert, I would have expected a non-bool "value" parameter to return DependencyObject.UnsetValue.

Similarly in ConvertBack, I would have expected again a non-T "value" parameter to return DependencyObject.UnsetValue, but also returning DependencyObject.UnsetValue for anything that doesn't equal TrueValue or FalseValue.
Left by Joe on Aug 29, 2013 6:15 PM

# re: A Generic Boolean Value Converter
Requesting Gravatar...
I am very much an amateur, but when I tried to use this in wpf, the XAML shows the error:
The property BoolToStyleConverter.FalseValue is not declared as an attachable property.
Left by Brent on Nov 01, 2013 1:13 AM

Your comment:
 (will show your gravatar)
 


Copyright © codingbloke | Powered by: GeeksWithBlogs.net | Join free