Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Converter does not update value in custom Frame derived object

galactosegalactose Member ✭✭

Hi guys,

Another ( :neutral: ) problem with custom elements... My purpose is to save many code lines by creating some custom elements that I permanently reuse... So this time, I tried to make a Frame derived class to put a tinted icon in. I use FFImageLoading with SVG image and a custom TintColor property, which is binded to an object's status => Color is given by a converter.

You have to know : all of this works perfectly when I "hard-code" it in my XAML page. The TintColor property works perfectly. The binding to the appropriate object's status (boolean Score.IsFavorite property) works perfectly. The converter works perfectly. But when I code it in the C# code-behind file, the TintColor property is not set correctly (and is set to default color for the first status).

public NavBarFavIcon()
{
    FavoritesStatusToColorConverter conv = new FavoritesStatusToColorConverter { BindingContext = this };
    conv.SetBinding(FavoritesStatusToColorConverter.NotFavColorProperty, nameof(NotFavColor));
    conv.SetBinding(FavoritesStatusToColorConverter.FavColorProperty, nameof(FavColorProperty));

    var icon = new TintedSvgCachedImage();
    icon.SetBinding(targetProperty: TintedSvgCachedImage.TintColorProperty,
                        new Binding(path: "IsFavorite", source: this.Score, converter: conv));
    icon.Source = SvgImageSource.FromResource("MyResource.svg");

    // I also use a TapGestureRecognizer to make the Score object's "IsFavorite" status update

    Content = icon;
}

Any idea ?
Thanks,
Galactose

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    It depends on how you consumed the binding.
    I assume the Score here is a bindable property so that it could be bound when using. And then the binding for icon should be assigned in the property changed:

    static void scoreChanged(BindableObject bindableObject, object oldValue, object newValue)
    {
        var frame = (NavBarFavIcon)bindableObject;
        var icon = (TintedSvgCachedImage)frame.Content;
    
        FavoritesStatusToColorConverter conv = new FavoritesStatusToColorConverter();
        conv.FavColor = Color.Red;
        conv.NotFavColor = Color.Green;
    
        icon.SetBinding(targetProperty: TintedSvgCachedImage.TintColorProperty,
                            new Binding(path: "IsFavorite", source: frame.Score, converter: conv));
    }
    

    Meanwhile, converter is a normal class we could define the normal properties there instead of a bindable property.
    I'm not very clear about your purpose but trying to create a simple project based on your description. Hope it is what you wanted:

Sign In or Register to comment.