Skip to content

Instantly share code, notes, and snippets.

@noutram
Last active December 20, 2019 13:44
Show Gist options
  • Save noutram/a82543e4f25f926aac340362238c72a8 to your computer and use it in GitHub Desktop.
Save noutram/a82543e4f25f926aac340362238c72a8 to your computer and use it in GitHub Desktop.
Xamarin Forms Value Converter
/*
...
xmlns:local="clr-namespace:NAMESPACE"
...
<ContentPage.Resources>
<ResourceDictionary>
<local:IntToBoolConverter x:Key="intToBool" />
</ResourceDictionary>
</ContentPage.Resources>
...
<Button Text="Hello"
IsEnabled="{Binding Source={x:Reference entry1}, Path=Text.Length, Converter={StaticResource intToBool}}" />
*/
public class IntToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value != 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? 1 : 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment