WPF: How to bind to a nested property?

后端 未结 3 1227
清歌不尽
清歌不尽 2020-12-05 06:54

I can bind to a property, but not a property within another property. Why not? e.g.



        
3条回答
  •  心在旅途
    2020-12-05 07:24

    Do both the ParentProperty and your class implement INotifyPropertyChanged?

        public class ParentProperty : INotifyPropertyChanged
        {
            private string m_ChildProperty;
    
            public string ChildProperty
            {
                get
                {
                    return this.m_ChildProperty;
                }
    
                set
                {
                    if (value != this.m_ChildProperty)
                    {
                        this.m_ChildProperty = value;
                        NotifyPropertyChanged("ChildProperty");
                    }
                }
            }
    
            #region INotifyPropertyChanged Members
    
            #endregion
        }
    
        public partial class TestClass : INotifyPropertyChanged
        {
            private ParentProperty m_ParentProperty;
    
            public ParentProperty ParentProperty
            {
                get
                {
                    return this.m_ParentProperty;
                }
    
                set
                {
                    if (value != this.m_ParentProperty)
                    {
                        this.m_ParentProperty = value;
                        NotifyPropertyChanged("ParentProperty");
                    }
                }
            }
    }
        public TestClass()
    
        {
            InitializeComponent();
            DataContext = this;
            ParentProperty = new ParentProperty();
            ParentProperty.ChildProperty = new ChildProperty();
    
            #region INotifyPropertyChanged Members
            #endregion
        }
    

提交回复
热议问题