INotifyPropertyChanged for static variable

前端 未结 2 1992
北海茫月
北海茫月 2020-12-09 21:13

I had a variable which was not static and INotifyPropertyChanged implemented succesfully. Then I tried to make it global, so turned it a static variable. But this time, INot

2条回答
  •  囚心锁ツ
    2020-12-09 22:16

    Very good example, I used it for some general settings in application, when I want to bind some property online to components

        public sealed class DataGridClass:INotifyPropertyChanged
    {
          private static readonly DataGridClass instance = new DataGridClass();
          private DataGridClass() { }
    
          public static DataGridClass Instance
           {
              get 
              {
                 return instance; 
              }
           }
    
        private int _DataGridFontSize {get;set;}
    
        public int DataGridFontSize
        {
            get { return _DataGridFontSize; }
            set { _DataGridFontSize = value;
                RaisePropertyChanged("DataGridFontSize");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    

    Set startup properties:

    DataGridClass.Instance.DataGridFontSize = 14(or read from xml)
    

    Bind this to components properties

    xmlns:static="clr-namespace:MyProject.Static"
    
    
        
    
    

    When you change this value somewhere in application like "Preferences"->DataGrid FontSize - to it automatically update this property for bindings with UpdateSourceTrigger

        private void comboBoxFontSize_DropDownClosed(object sender, EventArgs e)
        {
            DataGridClass.Instance.DataGridFontSize = Convert.ToInt32(comboBoxFontSize.Text);
        }
    
    
       
    

提交回复
热议问题