Search code examples
c#xamarinxamarin.formsxamarin.android

Background color not binding with a property


I can't bind a background color to my property located in my code behind.

My property:

public Color SelectedColor
    {
        get
        {
            return selectedColor;
        }

        set
        {
            pdfViewerControl.AnnotationSettings.FreeText.TextColor = value;
            selectedColor = value;
            // Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged();
        }
    }

My Button:

Button colorButton = new Button();
        colorButton.CornerRadius = 20;
        colorButton.BorderWidth = 2;
        colorButton.HeightRequest = 30;
        colorButton.WidthRequest = 30;
        colorButton.BindingContext = this;
        colorButton.SetBinding(Button.BorderColorProperty, new Binding("Value", source: SelectedColor));
        colorButton.SetBinding(Button.BackgroundColorProperty, new Binding("BackgroundColor", source: SelectedColor));

I tried different binding context but it doesn't work.

Both sections of code are on the same page.


Solution

  • if your property is named SelectedColor, then you should use that as the binding path when creating the binding

    colorButton.SetBinding(Button.BackgroundColorProperty, new Binding("SelectedColor"));