开发者

Binding TextBlock to complex properties

开发者 https://www.devze.com 2023-01-21 03:24 出处:网络
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.

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.


ClassB do not raise a property change notification!

0

精彩评论

暂无评论...
验证码 换一张
取 消