Just a small piece of code which might come in handy in a Silverlight or WPF project: The typical boolean to System.Visibility
converter.
Very often in one of your views, there is a piece of the view that must be shown or hidden in response to the value of a checkbox, for instance.
That’s where this converter comes in.
It also has an IsInverted
property, in case you want to hide something in case a certain value is true (and show it when it’s false).
Here’s the code:
using System.Windows;
using System.Windows.Data;namespace ValueConverters
{
///<summary>
/// Convertor to convert a boolean value to a Visibility value
/// (False = Collapsed, True = Visible)
///</summary>
public class BooleanToVisibilityConverter : DependencyObject, IValueConverter
{
///<summary>
/// Gets or sets a value indicating whether the visibility logic is inverted.
///</summary>
///<value>
/// <c>true</c> if the visibility logic is inverted; otherwise, <c>false</c>.
///</value>
public bool IsInverted
{
get { return (bool)GetValue(IsInvertedProperty); }
set { SetValue(IsInvertedProperty, value); }
}
///<summary>
/// Using a DependencyProperty as the backing store for IsInverted.
/// This enables animation, styling, binding, etc…
///</summary>
public static readonly DependencyProperty IsInvertedProperty =
DependencyProperty.Register(“IsInverted”,
typeof(bool),
typeof(BooleanToVisibilityConverter),
new PropertyMetadata(false));
///<summary>
/// Modifies the source data before passing it to the target for display in the UI.
///</summary>
///<param name=”value”>The source data being passed to the target.</param>
///<param name=”targetType”>The <see cref=”T:System.Type”/> of data expected by the target dependency property.</param>
///<param name=”parameter”>An optional parameter to be used in the converter logic.</param>
///<param name=”culture”>The culture of the conversion.</param>
///<returns>
/// The value to be passed to the target dependency property.
///</returns>
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
var result = Visibility.Visible;
if (value is bool)
{
if (IsInverted)
{
result = (bool)value ? Visibility.Collapsed : Visibility.Visible;
}
else
{
result = (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
}
return result;
}
///<summary>
/// Modifies the target data before passing it to the source object.
/// This method is called only in <see cref=”F:System.Windows.Data.BindingMode.TwoWay”/> bindings.
///</summary>
///<param name=”value”>The target data being passed to the source.</param>
///<param name=”targetType”>The <see cref=”T:System.Type”/> of data expected by the source object.</param>
///<param name=”parameter”>An optional parameter to be used in the converter logic.</param>
///<param name=”culture”>The culture of the conversion.</param>
///<returns>
/// The value to be passed to the source object.
///</returns>
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
var result = true;
if (value is Visibility)
{
if (IsInverted)
{
result = (Visibility)value == Visibility.Collapsed;
}
else
{
result = (Visibility)value == Visibility.Visible;
}
}
return result;
}
}
}