Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notifying ViewModel of Property change from Model class

In my WPF application, I have my TreeView IsSelected property binded to a property in my Model class. So the selected Item is set in the Model class. I need to notify my ViewModel whenever the selected Item is set/changed. How can I do that?

Thanks in advance.

like image 861
WAQ Avatar asked Jan 22 '26 13:01

WAQ


1 Answers

I guess your Model instance is part of your ViewModel... First, yes it should implement INotifyPropertyChanged. If you also want your ViewModel to get notified, then you ViewModel should subscribe to that event.

public class Model : INotifyPropertyChanged
{
   private string _name;
   public string Name {
      get {return _name;}
      set {
         _name = value;
         NotifyPropertyChanged("Name");
   }
// etc... including INPC implementation
}

public class ViewModel : INotifyPropertyChanged {
   public ViewModel (Model model){
      this.MyModel = model;
      this.MyModel.PropertyChanged += (s,e) => { DoSomething();};
   }

   public Model MyModel { get; set; }
}
like image 131
New Dev Avatar answered Jan 24 '26 12:01

New Dev