Skip to content

Instantly share code, notes, and snippets.

@luismts
Forked from QiMata/BooleanToObjectConverter.cs
Created December 12, 2020 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luismts/7d0f391e078a02ced0401276da542e64 to your computer and use it in GitHub Desktop.
Save luismts/7d0f391e078a02ced0401276da542e64 to your computer and use it in GitHub Desktop.
A list of useful converters for Xamarin Forms
public class BooleanToObjectConverter : BindableObject, IValueConverter
{
public static readonly BindableProperty TrueObjectProperty = BindableProperty.Create<BooleanToObjectConverter, Object>(x => x.TrueObject, null);
public static readonly BindableProperty FalseObjectProperty = BindableProperty.Create<BooleanToObjectConverter, Object>(x => x.FalseObject, null);
public object FalseObject
{
get { return GetValue(FalseObjectProperty); }
set { SetValue(FalseObjectProperty, value); }
}
public object TrueObject
{
get { return GetValue(TrueObjectProperty); }
set { SetValue(TrueObjectProperty, value); }
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
return TrueObject;
}
else
{
return FalseObject;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == TrueObject)
{
return true;
}
else
{
return false;
}
}
}
public class CollectionHasItemsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((IEnumerable<object>) value).Any();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class EqualsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class InvertBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool) value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class NullToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class ToStringConverter : BindableObject, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double && parameter is string)
{
return ((double)value).ToString((string)parameter);
}
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType == value.GetType())
{
return value;
}
if (targetType == typeof (int))
{
return System.Convert.ToInt32(value);
}
if (targetType == typeof(decimal))
{
return System.Convert.ToDecimal(value);
}
if (targetType == typeof(double))
{
return System.Convert.ToDouble(value);
}
if (targetType == typeof (string))
{
return System.Convert.ToString(value);
}
throw new ArgumentException($"Unable to convert value {value}",nameof(value));
}
}
public class ValueConverterGroup : List<IValueConverter>, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return this.Aggregate(value, (current, converter) => converter.Convert(current, targetType, parameter, culture));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment