BindingContextChanged OldValue NewValue

Summary

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; 
             }

        }
0
0 votes

Open · Last Updated

Posts

  • 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="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="Xamarin.Forms.Xaml.UnitTests.BindingContexEvents">
      <ContentView.Content>
          <StackLayout>
              <Button Text="Hello Xamarin.Forms!" 
                     x:Name="Button"/>
          </StackLayout>
      </ContentView.Content>
    </ContentView>
    
    using NUnit.Framework;
    using Xamarin.Forms.Core.UnitTests;
    
    namespace Xamarin.Forms.Xaml.UnitTests
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class BindingContexEvents : ContentView
        {
            public BindingContexEvents()
            {
                InitializeComponent();
            }
    
            class Test: BaseTestFixture
            {
                [Test]
                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.