Suppose I have 2 VM classes that implement INotifyPropertyChanged (or Prism NotificationObject in this case) and I want one VM to respond to a property changing in the other VM?
Suppose these are both child VMs as properties on a parent VM, what are some ways I could wire them up from the parent VM without using event aggregator / mediator?
I plan to have an ICommand (a PRISM DelegateCommand) exposed by the listening VM wired up to the PropertyChanged event of the other VM, via the parent VM.
Firstly, is this an acceptable way to do it and secondly, how do I wire-up an ICommand to an event? Do I have to use an attached behavior eg programmatic EventToCommand or similar or is there a more straight-forward way to do it?
Secondly, is this an acceptable way to do this, within the philosophy of MVVM?
It’s more straight-forward — since your event listener has full access to the target class, you can invoke the method/command directly.
Or, if the target needs to be an
ICommandand not a regular method, then usetargetVM.Command.Execute(null);instead.I would also suggest creating a custom event in your source class, so that you’re not relying on
OnPropertyChangedand the property name “magic string”.As far as the philosophy of MVVM and good design, I think it does deviate somewhat, because now your two view models are more tightly coupled. That’s not to say the approach is bad necessarily, but it does seem like Prism’s event aggregator might be a better choice.