BindingContextChanged OldValue NewValue

wesharpwesharp ITMember ✭✭


When BindingContext changed, allow intercept old and new value.

API Changes

public class BindablePropertyChangedEventArgs
    public object NewValue {get; Internal sets; }
    public object OldValue {get; Internal sets; }
    public BindableProperty Property {get; Internal sets; }

public event EventHandler <BindablePropertyChangedEventArgs> BindingContextChanged;

and use like this:

bindableObject.BindingContextChanged+= (s,e)=>
             var oldValue = e.OldValue as  INotifyPropertyChanged;
             var newValue = e.NewValue as  INotifyPropertyChanged;            
             if ( oldValue != null)
                   oldValue.PropertyChanged -= onPropertyChangedHandler; 
             if ( newValue != null)
                   newValue.PropertyChanged += onPropertyChangedHandler; 


Open · Last Updated


  • rogiheerogihee NLMember ✭✭✭

    Great addition! I really needed this sometimes too.

  • wesharpwesharp ITMember ✭✭

    On my forked of Xamarin.Forms , i have create two branch with different approc:

    • In the first i have implemented BindablePropertyChangedEventArgs and BindingPropertyChangedEventHandler
    • In the second i have using the existing delegate BindingPropertyChangedDelegate.

    What is the best approach?

  • wesharpwesharp ITMember ✭✭

    Any news?

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    BindingContext is a bindable property, so you can listen to both PropertyChanging and PropertyChanged events. Do the cleanup (remove event handler, e.g.) in PropertyChanging and build up in PropertyChanged

  • wesharpwesharp ITMember ✭✭
    edited December 2017

    Hi @StephaneDelcroix,
    I followed his suggestion, but I found a strange behavior.
    When have a BindableObject that inherit the DataContext and you change DataContext of parent , child object fired BindingContextChanged event but not PropertyChanging and PropertyChanged.
    It is the right behavior or it's bug?

    This sample test:

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentView xmlns="" 
              <Button Text="Hello Xamarin.Forms!" 
    using NUnit.Framework;
    using Xamarin.Forms.Core.UnitTests;
    namespace Xamarin.Forms.Xaml.UnitTests
        public partial class BindingContexEvents : ContentView
            public BindingContexEvents()
            class Test: BaseTestFixture
                public void ParentAndChildBindingContextChanged()
                    int countPropertyChanged = 0
                        , countDataContexChanged = 0
                        , countPropertyChanging = 0;
                    var parent = new BindingContexEvents();
                    var child = parent.Button;
                    child.PropertyChanging += (sender, args) => { if (args.PropertyName == "BindingContext") ++countPropertyChanging; };
                    child.PropertyChanged += (sender, args) => { if (args.PropertyName == "BindingContext") ++countPropertyChanged; };
                    child.BindingContextChanged += (sender, args) => { ++countDataContexChanged; };
                    parent.BindingContext = new object();
                    Assert.AreEqual(1, countDataContexChanged);
                    Assert.AreEqual(1, countPropertyChanging);
                    Assert.AreEqual(1, countPropertyChanged);               
  • wesharpwesharp ITMember ✭✭

    I have published a PR.

  • wesharpwesharp ITMember ✭✭
    edited December 2017

    Hi @StephaneDelcroix ,
    I understand your answer, but you can explain how I do you do intercept the value of the BindingContext before and after the change in a BindableObjcet that inherit BindingContext?

Sign In or Register to comment.