Skip to content

Instantly share code, notes, and snippets.

@HeathHopkins
Forked from QiMata/ValueConverterGroup.cs
Created February 23, 2018 13:37
Show Gist options
  • Save HeathHopkins/637b10c9a844fbb2874235c75895b22e to your computer and use it in GitHub Desktop.
Save HeathHopkins/637b10c9a844fbb2874235c75895b22e to your computer and use it in GitHub Desktop.
Reuse converters by chaining them together with a custom converter.
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();
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="someclass">
<ContentPage.Resources>
<ResourceDictionary>
<!-- Converters -->
<ValueConverterGroup x:Key="SelectionBackgroundColorConverter">
<EqualsConverter></EqualsConverter>
<BooleanToColorConverter TrueColor="{StaticResource GreenBackgroundColor}"
FalseColor="{StaticResource GraySeperatorColor}"/>
</ValueConverterGroup>
</ResourceDictionary>
</ContentPage.Resources>
</ContentPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment