4 minutes
Useful WPF Data Converters
Just sharing the most common WPF data binding converters that I encounter on day to day basis. This should be beneficial for every WPF developer.
Usage
Usage in XAML should be as follows:
Define the XAML namespace to point to the common namespace where you would store your common converters. For example:
<Window ... xmlns:converters="clr-namespace:MyCompany.MyProject.MyConverters" >
Use converter like this:
<Button Content="Push Me" Click="ButtonConvertToBool_Click" Visibility="{Binding Path=Something, Converter={x:Static converters:BoolToVisibleConverter.Instance}}" IsEnabled="{Binding Path=SomeOtherThing, Converter={x:Static converters:IsNotNullConverter.Instance}}" />
Common Converters
IsNullConverter
Gets an object as an input and returns true if the object is not null; otherwise returns false.
public class IsNullConverter : IValueConverter
{
private static readonly IsNullConverter _instance = new IsNullConverter();
public static IsNullConverter Instance
{
get { return IsNullConverter._instance; }
}
private IsNullConverter()
{
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
return (value == null);
}
catch
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
IsNotNullConverter
Gets an object as an input and returns true if the object is null; otherwise returns false.
public class IsNotNullConverter : IValueConverter
{
private static readonly IsNotNullConverter _instance = new IsNotNullConverter();
public static IsNotNullConverter Instance
{
get { return IsNotNullConverter._instance; }
}
private IsNotNullConverter()
{
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
return (value != null);
}
catch
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
BoolToVisibleConverter
Gets a boolean as an input and returns Visibility.Visible if the boolean is true; otherwise returns Visibility.Collapsed.
public class BoolToVisibleConverter : IValueConverter
{
private static readonly BoolToVisibleConverter _instance = new BoolToVisibleConverter();
public static BoolToVisibleConverter Instance
{
get { return BoolToVisibleConverter._instance; }
}
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if ((bool)value)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
catch
{
return null;
}
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
BoolToVisibleExConverter
Gets a boolean as an input and returns Visibility.Visible if the boolean is true; otherwise returns Visibility.Hidden.
public class BoolToVisibleExConverter : IValueConverter
{
private static readonly BoolToVisibleExConverter _instance = new BoolToVisibleExConverter();
public static BoolToVisibleExConverter Instance
{
get { return BoolToVisibleExConverter._instance; }
}
private BoolToVisibleExConverter()
{
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
if ((bool)value)
return Visibility.Visible;
else
return Visibility.Hidden;
}
catch
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
BoolToInvisibleConverter
Gets a boolean as an input and returns Visibility.Collapsed if the boolean is true; otherwise returns Visibility.Visible.
public class BoolToInvisibleConverter : IValueConverter
{
private static readonly BoolToInvisibleConverter _instance = new BoolToInvisibleConverter();
public static BoolToInvisibleConverter Instance
{
get { return BoolToInvisibleConverter._instance; }
}
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
if ((bool)value)
return Visibility.Collapsed;
}
return Visibility.Visible;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
InvertBoolConverter
Gets a boolean as an input and returns true if the boolean is false; otherwise returns false.
public class InvertBoolConverter : IValueConverter
{
private static readonly InvertBoolConverter _instance = new InvertBoolConverter();
public static InvertBoolConverter Instance
{
get { return InvertBoolConverter._instance; }
}
private InvertBoolConverter()
{
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
return !((bool)value);
}
catch
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
EnumDescriptionConverter
Gets an enum object as an input and returns a string defined in the description attribute if defined; otherwise returns .ToString().
public class EnumDescriptionConverter : IValueConverter
{
private static readonly EnumDescriptionConverter _instance = new EnumDescriptionConverter();
public static EnumDescriptionConverter Instance
{
get { return EnumDescriptionConverter._instance; }
}
private EnumDescriptionConverter()
{
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
catch
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}