2

I'm using a bindable property like this in a class the inherits from Xamarin.Forms.ContentView:

public static readonly BindableProperty OverlayColorProperty = BindableProperty.Create(nameof(OverlayColor), typeof(Color), typeof(MyControl), Color.FromHex("#55000000"));

public Color OverlayColor
{
    get => (Color)GetValue(OverlayColorProperty);
    set => SetValue(OverlayColorProperty, value);
}

Furthermore I'm listening for changes to update an inner elements background color:

protected override void OnPropertyChanged(string propertyName)
{
    base.OnPropertyChanged(propertyName);

    switch (propertyName)
    {
        case nameof(OverlayColor):
            GridBackground.BackgroundColor = OverlayColor;
            break;
    }
}

I just noticed that OnPropertyChanged does not get called with the default value. Just when I update it from another place, XAML or through code.

Is this exspected behavior? If yes, why? What should I do instead? Define it also in XAML code?

Gabriel Weidmann
  • 756
  • 12
  • 16
  • why aren't you just binding GridBackground.BackgroundColor? – Jason May 14 '20 at 16:17
  • @Kithoras According to [Xamarin.Forms Bindable Properties](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/bindable-properties#detect-property-changes), we can see that propertyChanged callback method will be invoked when the value of the bindable property changes. – Cherry Bu - MSFT May 15 '20 at 05:46
  • @Jason GridBackground is a nested control. I cannot bind it from outside. This is the reason I'm using this in-between BindableProperty. – Gabriel Weidmann May 15 '20 at 09:48
  • @CherryBu-MSFT Yes, and...? Of course I can try this, but my question is if this is a bug or exspected behavior. I also have other ideas for workarounds, but I would like to understand this thing :-) – Gabriel Weidmann May 15 '20 at 09:50
  • @KithorasCarzyl I think it is a expected behavior, and have no problem. – Cherry Bu - MSFT May 18 '20 at 00:50

0 Answers0