How WPF framework handles cyclic update of properties in MVVM?

后端 未结 3 1513
無奈伤痛
無奈伤痛 2021-01-22 16:10

Lets assume property Name is bind to TextBox in view like this.

private string name
public string Name
{
   get {return name;}
   set {
       name=value;
              


        
3条回答
  •  [愿得一人]
    2021-01-22 16:29

    A standard implementation of a property should look like this:

    private string name;
    
    public string Name
    {
       get { return name; }
    
       set
       {
           if( name != value )
           {
               name = value;
               OnPropertyChanged("Name");
           }
       }
    }
    

    Note the additional if to make sure the event is only raised if the value of the property actually changed.

提交回复
热议问题