Skip to content

Instantly share code, notes, and snippets.

@YoungjaeKim
Created June 11, 2013 03:22
Show Gist options
  • Save YoungjaeKim/5754287 to your computer and use it in GitHub Desktop.
Save YoungjaeKim/5754287 to your computer and use it in GitHub Desktop.
Inversed Boolean Converter for XAML.
public class InverseBooleanConverter : IValueConverter
{
/// <summary>
/// Converts a Boolean value as inversed.
/// </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">parameter for null value handling. if <c>false</c>, returns false when null. Or if <c>true</c>, returns true when null. Otherwise bypass the null value.</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='false' then treat null value as false.
var args = value as bool?;
var param = parameter.ToString().ToLower();
if (args == null)
{
if(param.Contains("false"))
return false;
if(param.Contains("true"))
return true;
return null;
}
return !args;
}
/// <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