将TextBlock绑定到复杂属性

时间:2010-10-25 18:25:07

标签: wpf binding

YS, 我需要帮助理解为什么字段没有更新: 我有两个样本类都派生自Observable,只是实现了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";
        }
    }

当我更改ClassB中的属性时,绑定到Property2的TextBlock不会更新。但是,更新了绑定到Property2Representation的TextBlock。我做错了什么?

TIA。

1 个答案:

答案 0 :(得分:0)

ClassB不会提出属性更改通知!