While developing applications in Wpf, sometimes we follow the MVVM pattern and come across the requirement to bind commands. We'll discuss the same in this blog. And for demo purpose, I've implemented command binding in Wpf C1DataGrid. Our motive is to enable row selection using MVVM. Let's create a new Wpf project "CommandBinding" and add three folders - View, Model and ViewModel - to it. Then add a new class called “Employee” under the "Model" folder. This is our domain entity and contains a few properties.
Add a class for ICommand Implementation called “DelegateCommand", to the folder "ViewModel".
Add another class "EmployeeListView" to the folder "ViewModel". This is responsible for handling commands that get triggered in the view; and also, for sending data to the view.
Now, drag MainWindow.xaml to folder "View". Once done, open App.xaml and change "StartupUri" tag from "MainWindow.xaml" to "View/MainWindow.xaml". Take a look at the XAML markup for MainWindow.xaml where we place C1DataGrid, add columns, set bindings and add "MouseLeftButtonUp" event trigger using Interactivity.
And this is it :) Now, when you click on any row, the row gets selected with a message box that displays the value of a field from the selected DataItem. Download C# Sample Download Vb.Net Sample