tags:

views:

23

answers:

1

ys, 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";
        }
    }

When I change a property in ClassB, the TextBlock bound to Property2 is not updated. However, TextBlock bound to Property2Representation is updated. What am I doing wrong?

TIA.

A: 

ClassB do not raise a property change notification!

rudigrobler
Sorry, my bad, but ClassB does raise property change notification on its Property1. I somehow forgot to add that.
e28Makaveli
Have you bound Property2 as source or path, if it is bound as source then it won't work because the moment you update Property2 you are changing its reference.
bjoshi
Tried binding to path but that did not do it. In the mean time, I simply created another string property that raises property changed to get updates from there.
e28Makaveli