mshwf
mshwf

Reputation: 7449

Can two Bindable properties communicate with each other?

I'm creating a control (deriving from ContentView) which has a Frame field:

public class SelectionFrame : ContentView
{
    Frame f1;
        public SelectionFrame()
        {
            f1 = new Frame
            {
                VerticalOptions = LayoutOptions.Center,
                BackgroundColor = BorderColor,
                HorizontalOptions = LayoutOptions.Center,
                Padding = new Thickness(0),
                CornerRadius = 7,
                HeightRequest = 14,
                WidthRequest = 14,
                Content = f2
            };
            Content = f1;
        }

I set its BackgroundColor through this BindableProperty:

   public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(SelectionFrame), defaultValue: Color.Black, defaultBindingMode: BindingMode.TwoWay, propertyChanged: ColorChanged);

   public Color BorderColor
        {
            get { return (Color)GetValue(BorderColorProperty); }
            set { SetValue(BorderColorProperty, value); }
        }

then I use this control inside another custom control (RadioButtonsGroup), that has this FrontColor BindableProperty:

      public static readonly BindableProperty FrontColorProperty = BindableProperty.Create(nameof(FrontColor), typeof(Color), typeof(RadioButtonsGroup), defaultValue: Color.Black);

  public Color FrontColor
        {
            get { return (Color)GetValue(FrontColorProperty); }
            set { SetValue(FrontColorProperty, value); }
        }

And here I set the BorderColor of the SelectionFrame :

SelectionFrame circle = new SelectionFrame { ClassId = "r", BorderColor = FrontColor, VerticalOptions = LayoutOptions.Center };

I use the control like so:

<local:RadioButtonsGroup FrontColor="Red" x:Name="rbs"/>

but the BackgroundColor of the frame is not changed and still has the default color (Black).

Upvotes: 1

Views: 57

Answers (1)

Diego Rafael Souza
Diego Rafael Souza

Reputation: 5313

It's because you didn't bound the BackgroundColor frame's property to your custom control property. You set it up on the constructor, bur you aren't listening for changes, what you are doing in this line:

SelectionFrame circle = new SelectionFrame { ClassId = "r", BorderColor = FrontColor, VerticalOptions = LayoutOptions.Center };

To correct it, just change your SelectionFrame constructor's code to this:

public SelectionFrame()
{
    f1 = new Frame
    {
        VerticalOptions = LayoutOptions.Center,
        HorizontalOptions = LayoutOptions.Center,
        Padding = new Thickness(0),
        CornerRadius = 7,
        HeightRequest = 14,
        WidthRequest = 14,
        Content = f2
    };

    f1.SetBinding(Frame.BackgroundColorProperty, new Binding(nameof(BorderColor)));
    f1.BindingContext = this;

    Content = f1;
}

Now you will be able to listen the changes to this property.

I hope it helps.

Upvotes: 1

Related Questions