In MVVM, if View wants to communicate with View Model, it is through commands. Commands are essentially classes that implement the
ICommand interface. However, creating a separate class for each command required by the View makes the code base unnecessarily large. In this recipe we will develop a generic command class; the instances of which can be used by different View controls, such as buttons, to invoke a specific logic within View Model.
We will also look at how to bind data present in View Model to a control in View. We will accomplish this by binding user data in
UserViewModel to the
DataGrid control in
MainWindow.xaml, which is our View.