Bindable Property In IValueConverter Always Null

Viewed 134

I have a converter that deals with Boolean values and uses them to pick either of two ImageSources. I defined the ImageSource as two parameters in the converter, later on I need to supply those resources using the DynamicRsource markup extension from the XAML so I devised the code below

public class BooleanToImageSourceConverter : BindableObject, IValueConverter
{
    public static readonly BindableProperty TrueImageSourceProperty = BindableProperty.Create(nameof(TrueImageSource), typeof(ImageSource), typeof(BooleanToImageSourceConverter));
    public static readonly BindableProperty FalseImageSourceProperty = BindableProperty.Create(nameof(FalseImageSource), typeof(ImageSource), typeof(BooleanToImageSourceConverter), propertyChanged: Test);

    private static void Test(BindableObject bindable, object oldValue, object newValue)
    {
        if (oldValue == newValue)
            return;

        var control = (BooleanToImageSourceConverter)bindable;
    }

    public ImageSource TrueImageSource
    {
        get => (ImageSource)GetValue(TrueImageSourceProperty);
        set => SetValue(TrueImageSourceProperty, value);
    }
    public ImageSource FalseImageSource
    {
        get => (ImageSource)GetValue(FalseImageSourceProperty);
        set => SetValue(FalseImageSourceProperty, value);
    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool isTrue = (bool) value;
        return isTrue ? TrueImageSource : FalseImageSource;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == TrueImageSource;
    }
}

XAML

<converters:BooleanToImageSourceConverter
    x:Key="FavImageSourceConverter"
    FalseImageSource="{DynamicResource savedToFav}"
    TrueImageSource="{DynamicResource saveToFav}" />

<ImageButton
    BackgroundColor="Transparent"
    Command="{Binding SetFavCommand}"
    HorizontalOptions="End"
    Source="{Binding IsFavorite, Converter={StaticResource FavImageSourceConverter}}" />

although I can see in the property changed event that a new value is being set whenever the convert method is called I can see that both image sources are null. Am I doing something wrong here? or is it just not possible by design?

Please note that I can not use triggers to do this because of some internal reasons to the application

0 Answers
Related