Created
June 10, 2013 18:23
-
-
Save YoungjaeKim/5751033 to your computer and use it in GitHub Desktop.
More usable Boolean to Visibility converter for XAML converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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