Skip to content

Instantly share code, notes, and snippets.

@LambdaSix
Created February 7, 2013 11:38
Show Gist options
  • Save LambdaSix/4730435 to your computer and use it in GitHub Desktop.
Save LambdaSix/4730435 to your computer and use it in GitHub Desktop.
Convert between String and Boolean? in a bad way.
/// <summary>
/// Convert between Boolean? and String
/// </summary>
public class BooleanToStringConverter : IValueConverter
{
public object Convert( object value, Type targetType, object parameter, CultureInfo culture)
{
// bool? to string.
bool castValue = (value as bool?) ?? false;
return castValue ? "True" : "False";
}
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture)
{
string castValue = (value as String) ?? "False";
bool outVal = false;
Boolean.TryParse(castValue, out outVal);
return outVal as bool?;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment