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.

binding to a property in a custom control isn't being updated when source changes

RainerMagerRainerMager JPMember ✭✭✭

I've created a custom control that in basically a form to input credit card information. I expose that info via a single CreditCard BindableProperty, which is a CreditCard object holding all the the details from the form. The code behind for that property is:

            public CreditCard CreditCard
            {
                get => (CreditCard)GetValue(CreditCardProperty);
                set => SetValue(CreditCardProperty, value);
            }
            public static readonly BindableProperty CreditCardProperty
                = BindableProperty.Create(
                    nameof(CreditCard),
                    typeof(CreditCard),
                    typeof(CreditCardInput),
                    CreditCard.Empty,
                    BindingMode.TwoWay,
                    null,
                    (bindable, oldValue, newValue) => HandlePropertyChanged(nameof(CreditCard), bindable, oldValue, newValue)
                );    

Then in my HandlePropertyChanged method I set the value of CreditCard so that when my control is created it gets the initial value:

        private static void HandlePropertyChanged(string name, BindableObject bindable, object oldValue, object newValue)
        {
            var creditCardInput = (CreditCardInput)bindable;
            switch(name) {
                case nameof(CreditCard):
                    creditCardInput.CreditCard = (CreditCard)newValue;
                    break;
            }
        }

This works fine in that I can use this control in a page, say MyPage, and bind to the CreditCard property and that binding's source object get's updated as I enter text into the CreditCardInput's fields. Something like this:

                <controls:CreditCardInput CreditCard="{Binding CreditCard}" />

The CreditCard property in MyPage is simply:

        private CreditCard creditCard = CreditCard.Empty;
        public CreditCard CreditCard
        {
            get => creditCard;
            set = { creditCard = value; OnPropertyChanged(nameof(CreditCard)); }
        }

My problem is, if I change the value of CreditCard in MyPage then that is not reflected in CreditCardInput. I've tried debugging and CreditCardInput doesn't seem to receive any events when I change CreditCard. Specifically, I would expect `HandlePropertyChanged' to get called.

I've done a lot of searching and found a number of examples of custom controls, but none of them show examples when the custom control is used and its properties and then bound to something. Instead, they all show just static values being passed in.

Does anyone have any ideas?

Best Answer

  • RainerMagerRainerMager JPMember ✭✭✭
    Accepted Answer

    Never mind, I figured it out. My problem was an unrelated bug. When I was updating CreditCard in MyPage it was using an old value. I think there are checks in binding logic that if the old value and new value are the same then messages aren't propagated, so that's why I wasn't seeing the events happening.

Answers

  • RainerMagerRainerMager JPMember ✭✭✭
    Accepted Answer

    Never mind, I figured it out. My problem was an unrelated bug. When I was updating CreditCard in MyPage it was using an old value. I think there are checks in binding logic that if the old value and new value are the same then messages aren't propagated, so that's why I wasn't seeing the events happening.

Sign In or Register to comment.