Simple small INotifyPropertyChanged implementation

后端 未结 6 1496
深忆病人
深忆病人 2021-01-06 07:51

Say I have the following class:

public MainFormViewModel
{
    public String StatusText {get; set;}
}

What is the easiest smallest way to g

6条回答
  •  执笔经年
    2021-01-06 08:13

    Ive always liked this method

    private string m_myString;
    public string MyString
    {
        get { return m_myString; }
        set 
        {
            if (m_myString != value)
            {
                 m_myString = value;
                 NotifyPropertyChanged("MyString");
            }
        }
    }
    
    
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
             PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
    

    or for less code bloat

    set 
    {
        m_myString = value;
        NotifyPropertyChanged("MyString");
    }
    

提交回复
热议问题