Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save YoungjaeKim/5751033 to your computer and use it in GitHub Desktop.
Save YoungjaeKim/5751033 to your computer and use it in GitHub Desktop.
More usable Boolean to Visibility converter for XAML converter
public class NullableBooleanToVisibilityConverter : IValueConverter
{
/// <summary>
/// Converts a value.
/// </summary>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
/// <param name="value">The value produced by the binding source.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">Conditional parameter <c>null</c>, only visible when null. <c>true</c>, only visible when true.</param>
/// <param name="culture">The culture to use in the converter.</param>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//i.e: put parameter as 'true+collapse' says Visible when true, otherwise its collapsed.
var args = value as bool?;
var param = parameter.ToString().ToLower();
if (args == null && param.Contains("null"))
return Visibility.Visible;
if (args == true && param.Contains("true"))
return Visibility.Visible;
if (args == false && param.Contains("false"))
return Visibility.Visible;
return param.Contains("collapse") ? Visibility.Collapsed : Visibility.Hidden;
}
/// <summary>
/// Converts a value.
/// </summary>
/// <returns>
/// A converted value. If the method returns null, the valid null value is used.
/// </returns>
/// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment