Binding static property and implementing INotifyPropertyChanged

前端 未结 3 1232
温柔的废话
温柔的废话 2021-01-06 07:54

I\'m trying to bind a static property of some class to some control. I\'ve tryied a few implementation but each has its problem:

All examples use the next XAML:

3条回答
  •  梦谈多话
    2021-01-06 08:36

        public static String StatusInformation
        {
            get { return _StatusInformation; }
            set { _StatusInformation = value; OnStaticPropertyChanged("StatusText"); }
        }
    
        #region Handlig Static Properties Changed
        private static System.ComponentModel.PropertyChangedEventHandler staticpropChanged;
        static protected void OnStaticPropertyChanged(string pname)
        {
            System.ComponentModel.PropertyChangedEventArgs e = new System.ComponentModel.PropertyChangedEventArgs(pname);
            System.ComponentModel.PropertyChangedEventHandler h = staticpropChanged;
            if (h != null)
                h(null, e);
    
        }
        private void Handler_PropertyChange(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            NotifyPropertyChanged(e.PropertyName);
        }
        #endregion
        public string StatusText
        {
            get { return ExchangeServices.StatusInformation; }
            set { ExchangeServices.StatusInformation = value; }
        }
    

    this way i didnt have to do any handling in the event at all. this was really helpfull to create one status bar for my entire program and update it from anywhere and any user control in my ever expanding program.

    Thank you to shimpossible

提交回复
热议问题