c# - MVVM: how model can use inotifypropertychanged to notify viewmodel of changes -


i need model notify viewmodel if property changed, because need collect changed model instances in collection further processing, enable , disable command buttons in viewmodel. i've used modelbase abstract class , added haschanges property can test against in viewmodel , catch changed models.but not working , don't know i'm missing.

public abstract class modelbase : inotifypropertychanged {     protected modelbase()     {     }      private bool _haschanges;     public bool haschanges     {                 {              return _haschanges;         }          set         {             if (_haschanges != value)             {                 _haschanges = value;                 raisepropertychanged("haschanges");             }         }     }      protected void raisepropertychanged(string propertyname)     {         haschanges = true;         this.onpropertychanged(new propertychangedeventargs(propertyname));     }      public event propertychangedeventhandler propertychanged;      protected virtual void onpropertychanged(propertychangedeventargs e)     {         var handler = this.propertychanged;          if (handler != null)         {             handler(this, e);         }     } } 

the model wrapped inside viewmodel , bound view datagrid:

private model_selectedmodel;  public mode selectedmodel {          {         return _selectedmodel;      }      set     {         if (_selectedmodel != value)         {             _selectedmodel = value;             notifypropertychanged("selectedmodel");         }     } } 

thanks valuable help.

i tested class , it's okay. think point typo here:

private model_selectedmodel;  public mode selectedmodel {          {         return _selectedmodel;      }      set     {         if (_selectedmodel != value)         {             _selectedmodel = value;             notifypropertychanged("selectedmodel");         }     } } 

there should raisepropertychanged instead of notifypropertychanged.

below it's test:

xaml

<window x:class="testupdatepropertychanged.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:this="clr-namespace:testupdatepropertychanged"         title="mainwindow" height="350" width="525">      <window.datacontext>         <this:testviewmodel />     </window.datacontext>      <grid>         <textbox width="100" height="25" text="{binding path=teststring, updatesourcetrigger=propertychanged}" />          <button width="100" height="30" verticalalignment="top" content="click" click="button_click" />     </grid> </window> 

code-behind

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();     }      private void button_click(object sender, routedeventargs e)     {         var testdata = this.datacontext testviewmodel;          testdata.teststring = "yay, it's change!";          if (testdata.haschanges == true)          {             messagebox.show("it's change!");         }     } }  public class testviewmodel : modelbase  {    private string _teststring = "test";      public string teststring     {         { return _teststring; }          set         {             if (_teststring != value)             {                 _teststring = value;                 raisepropertychanged("teststring");             }         }     } }   public abstract class modelbase : inotifypropertychanged {     protected modelbase()     {     }      private bool _haschanges;     public bool haschanges     {                 {              return _haschanges;         }          set         {             if (_haschanges != value)             {                 _haschanges = value;                 raisepropertychanged("haschanges");             }         }     }      protected void raisepropertychanged(string propertyname)     {         haschanges = true;         this.onpropertychanged(new propertychangedeventargs(propertyname));     }      public event propertychangedeventhandler propertychanged;      protected virtual void onpropertychanged(propertychangedeventargs e)     {         var handler = this.propertychanged;          if (handler != null)         {             handler(this, e);         }     } } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -