Skip to content

Instantly share code, notes, and snippets.

@QiMata
Created May 16, 2016 15:26
Show Gist options
  • Save QiMata/d40db719c98b0b4d1cb16ddb802f988d to your computer and use it in GitHub Desktop.
Save QiMata/d40db719c98b0b4d1cb16ddb802f988d 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();
}
}
@IeuanWalker
Copy link

The 'ValueConverterGroup' converter, is there a way to pass parameters to different converters?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment