Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Xamarin.forms Converters
public class BooleanToYesNoConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? AppResources.BtnYes : AppResources.BtnNo;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? AppResources.BtnYes : AppResources.BtnNo;
}
}
public class HasDataConverter : IValueConverter
{
/// <summary>
/// Init this instance.
/// </summary>
public static void Init()
{
DateTime time = DateTime.UtcNow;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//if null then not visible
if (value == null)
return false;
//if empty string then not visible
if (value is string)
return !string.IsNullOrWhiteSpace((string)value);
//if blank list not visible
if (value is IList)
return ((IList)value).Count > 0;
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class ListHeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return 0;
return (int)value * int.Parse(parameter.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return 0;
return (int)value * int.Parse(parameter.ToString());
}
}
public class ListNullOrEmptyBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is ICollection collection && collection.Count > 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is ICollection collection && collection.Count > 0;
}
}
public class NegateBooleanConverter : 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)
{
return !(bool)value;
}
}
public class PropertyValueStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class ValueConverterGroup : List<IValueConverter>, IValueConverter
{
private string[] _parameters;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter != null)
_parameters = Regex.Split(parameter.ToString(), @"(?<!\\),");
return (this).Aggregate(value, (current, converter) => converter.Convert(current, targetType, GetParameter(converter), culture));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
private string GetParameter(IValueConverter converter)
{
if (_parameters == null)
return null;
int index = IndexOf(converter);
string parameter;
try
{
parameter = _parameters[index];
}
catch (IndexOutOfRangeException ex)
{
parameter = null;
}
if (parameter != null)
parameter = Regex.Unescape(parameter);
return parameter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.