Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2815
Complex property that implements INotifyPropertyChanged does not update
posted

Guys,

I need help understanding why a field is not being updated:

I have two sample class both deriving from Observable that simply implements INotifyPropertyChanged.

public class ClassA : Observable

    {

        string p1;

        public string Property1

        {

            get { return p1; }

            set

            {

                p1 = value;

                RaisePropertyChangedEvent("Property1");

            }

        }

 

        ClassB p2;

        public ClassB Property2

        {

            get { return p2; }

            set

            {

                if (p2 != null)

                    p2.PropertyChanged -= OnProperty2Changed;

 

                p2 = value;

 

                if (p2 != null)

                    p2.PropertyChanged += OnProperty2Changed;

            }

        }

 

        string p2r;

        public string Propert2Representation

        {

            get { return p2r; }

            set

            {

                p2r = value;

                RaisePropertyChangedEvent("Propert2Representation");

            }

        }

 

        void OnProperty2Changed(object sender, System.ComponentModel.PropertyChangedEventArgs e)

        {

            this.RaisePropertyChangedEvent("Property2");

            this.Propert2Representation = Property2.ToString();

        }

 

        public override string ToString()

        {

            return "Class A";

        }

    }

 

    public class ClassB : Observable

    {

        public int Property1 { get; set; }

        public override string ToString()

        {

            return "Class B";

        }

    }

I have a list of ClassA items bound to the xamDataGrid.   When I  change a property in ClassB, the field bound to Property2 is not updated.  However, field bound to Property2Representation is updated.   I have looked at this for some time now but cannot figure it out.  Any ideas?

  • 9694
    posted

    Hi there,

    I see what you are trying to do. I created a sample which uses your code to demonstrate the problem. I did find an error in the code that you posted here. ClassB.Property1 needs to call RaisePropertyChangedEvent in its setter in order for this to work.

    With that change, the XamDataGrid does get notified to update Property2 (of type ClassB) when its Property1 property is changed (because of your specialized code). However, when there is no built in editor for the type, it does not call the ToString method on the notification update. This could be considered a bug. However, the strength of the XamDataGrid is in its editors which will do the right thing on a notification.

    Thus to fix this, you must define a Field with a CellValuePresenter or define an UnboundField with a BindingPath set up to point to Property1 in order for ClassB to work. In the attached sample, I have an UnboundField defined for Property2 (of type ClassB) where the BindingPath points to ClassB.Property1:

        <igDP:UnboundField Name="Property2" BindingPath="Property2.Property1"/>

    Also, to test this the sample uses a Timer to dynamically change the data at run time. Please let me know how we can help you further.

    Thanks!

    SampleGridData.zip