Created
May 16, 2016 15:26
-
-
Save QiMata/d40db719c98b0b4d1cb16ddb802f988d to your computer and use it in GitHub Desktop.
A list of useful converters for Xamarin Forms
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 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; | |
} | |
} | |
} |
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 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(); | |
} | |
} |
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 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(); | |
} | |
} |
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 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(); | |
} | |
} |
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 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(); | |
} | |
} |
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 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)); | |
} | |
} |
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 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
The 'ValueConverterGroup' converter, is there a way to pass parameters to different converters?