1. Do I need to have a new ViewModel for each view? If not, can a single MasterViewModel create an MVVM violation?
Not really. You can have specific ViewModels that correspond to a large number of views, each of which shows the same data in a different format. In fact, this is the whole rationale for MVVM in the first place - the segregation of displayed and business rules, so that the display format can be changed by loading different views.
You can also have one view that matches several other ViewModels. This is code reuse in the display user interface.
2. How will ViewModels interact with each other?
Typically, ViewModels interact with views through a WPF binding. This is why it is called MVVM, not MVC.
ViewModels can interact with each other through a number of standard .NET tools.
3. MainWindow.xaml.cs, where I integrate all the views, should only have a viewmodel initialization and a DataContext destination there, or can I put other codes too?
You usually split each view into a separate XAML file. This makes it easy to substitute a different view for a different format with the same data.
It is generally recommended that you separate your code from stand-alone modules; that is, one view of one file, one view mode of one file.
4. I have my specific EventHandlers. Should I use them in the ViewModel or outside the model-view-viewmodel?
Events should be processed in views if they are purely UI-driven (i.e. have nothing to do with data).
If the event should affect some changes in the underlying data (or do some actions in the business rules), you can in turn raise the event on the ViewModel. Please note that this event in the ViewModel may be different from the event in the view / interface.
Stephen chung
source share