There are three windows MainWindow, FirstWindow and SecondWindow. MainWindow can open FirstWindow and SecondWindow.
Now my question is:
- How to open SecondWindow from FirstWindow, and close FirstWindow when the SecondWindow open. At this time, I can control SecondWindow but can't control MainWindow, just like using SecondWindow.ShowDialog() from MainWindow.
- After I click the "save" button on SecondWindow, the SecondWindow shall be closed and the DataGrid of MainWindow shall be updated. How to update data from another ViewModel or how to return data when event was handled?
You need to use an instance of the form class to pass data. See my simple two form project below
Form 1
Form 2
I'm agree with Tseng's answer and will try to extend his answer.
First part
For low-coupled communication between modules (not only ViewModels) we can try to implement EventAggregator pattern. Event aggregator helps to implement subscriber/publisher pattern in low-coupled app. I know few different implementations.
First one based on CodeProject post and uses WeakReference that will help you to prevent memory leak. I will not publish whole code because you can just download source code and use it. In this implementation you must to implement ISubscriber interface for your subscribers.
The second is Microsoft Prism implementation. This is an open source project then you can see interface, implementation and base event class. In this implementation you must unsubscribe from the event manually.
The third and the last is MVVMLight library and its Messenger class.
As you can see all of this implementations uses Singleton pattern for saving subscribers.
Second part
Second part is about navigation. The simpliest way is to use Page navigation infrastructure. But in MVVM-world we have many different concepts for navigation.
The main purpose to use navigation abstractions is to separate navigation logic from concrete view rendering technology (WPF, Silverlight, WinRT, Xamarin).
For example, in Microsoft Prism we can use regions and RegionManager for navigation between views and windows. It's very ponderous navigation framework and it can be difficult to understand the concept after only one article.
MVVM Light also have their own navigation mechanism.
For my projects I use my own realization of navigation via Workspaces. It is a hybrid mechanism that combines Page navigation principes from .net and Regions concept from Prism.
Conclusions
This post is not an answer to your questions. But I hope that it will be helpful to you to understanding MVVM concepts.
As you can read above there are many MVVM-frameworks which contains infrastructure (not only Messenger and NavigationService, but also base command realisations, PopupService, converters, INotifyPropertyChanged-helpers and base ViewModel implementations) to implement typical scenarios in your application.
You are asking multiple for multiple things here.
Basically you need 2 things. An event aggregator (also called messenger) to pass messages between view models. There are different frameworks that implement it or they come as part of MVVM Frameworks.
Second you need is a navigation service to decouple navigation from your view models, as navigation requires knowledge of the view related technology (WPF, UWP, Silverlight etc.)