1
votes

I have a WPF app that uses a business object called Visit, it has a lot of child objects like patient, exam, and others. There are views and view models for editing these various child objects, so there is a view and view model for editing patient info and one set for exam info, etc. There is also a main window view model.

When I need to open a new Visit, I have a search screen that also has it's own view model. It needs to open new visit from the database and notify all the other views that the visit has changed.

I've looked into WeakEventManager, and also having one view model that is the parent of all the others, but I'm not sure what is the best way to proceed. I'd like to know what the relationship between the view models should be and how the open/search view model should tell all the other views to update. I have been calling OnPropertyChanged("propname") in my view models when a property is updated, but since the other views don't know about the open/search view model, they don't care if I say OnPropertyChanged("Visit")

2

2 Answers

2
votes

Have a look at this post on SO that talks about the Messenger. In your case you case post the Visit object and have the ViewModel capture that for display.

If you have a very data centric views where the Model data is pretty much presented directly on the View without a lot of modification you can easily expose the Model as a property on the ViewModel and have the View bind to its properties.

This way when one View updates the Model the other View's will update automatically without having to listen for property change events on the Model

Edit:

To elaborate on my second point: You may or may not need this but if your Model also implements INotifyPropertyChanged then any changes to that model will be propagated to the View automatically.

If you need to have 2 Views with the Visit object then you can have the Visit property directly bound in XAML

public class ViewModel1 : ViewModelBase
{
   public ViewModel1(IMessenger messenger)
   {
       messenger.Register<Visit>(this, (v) => CurrentVisit = v);
   }

   public Visit CurrentVisit
   {
      get { _visit; }
      set { _visit = value; RaiseNotifyPropertyChange("CurrentVisit"); }
   }
}

public class ViewModel2: ViewModelBase
{ 
   public ViewModel2(IMessenger messenger)
   {
       messenger.Register<Visit>(this, (v) => CurrentVisit = v);
   }

   public Visit CurrentVisit
   {
      get { _visit; }
      set { _visit = value; RaiseNotifyPropertyChange("CurrentVisit"); }
   }
}

public class CurrentVisit : INotifyPropertyChanged
{ ... }

Like I said this is only applicable if you need the same Visit object shared amounst 2 or more ViewModels and if the View's mostly are data centric or in other words the data from the Model is being presented directly on the screen. This is to avoid duplicating the properties in the ViewModels and having to raise the property change event all the time.

0
votes

use global variable. simply use a Property in your base ViewModel class whose set part set the global variable and get part return that variable value... for setting that global variable create a class in other common project and create a static public variable or property.