In this article, we will learn about button click event in WPF with MVVM concept. We use ICommand interface for generating the button click event. Let's take one simple example of one simple textbox and button. After writing the text in textbox, when we click on button, it will display a simple message box.
Before starting this article, please read Explain INotifyPropertyChanged In WPF - MVVM article first.
First, create one WPF application and create a window like the following.

XAML code
- <Window x:Class="MVVM_ICommand.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">
- <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
- <TextBox Width="120" Height="30" Margin="10"></TextBox>
- <Button Width="90" Margin="10" Content="Click Me"></Button>
- </StackPanel>
- </Window>
Give View-Model namespace reference to the View and define the window resource file. Then, give data context to the parent control and also, bind the text property of the text box.
Now, let’s start our main code for making a new class RelayCommand. Make it as public.
- public class RelayCommand {}
- //Now implement the interface ICommand
- public class RelayCommand: ICommand {
- public boolCanExecute(object parameter) {
- }
- public event EventHandlerCanExecuteChanged;
- public void Execute(object parameter) {
- }
- }
Now, Create two action properties that we initialize in constructor of the relay command class.
- Action < object > _execteMethod;
- Func < object,bool> _canexecuteMethod;
- public RelayCommand(Action < object > execteMethod, Func <object , bool> canexecuteMethod) {
- _execteMethod = execteMethod;
- _canexecuteMethod = canexecuteMethod;
- }
Here, in CanExecute method, we put null validation to check if _canexecutemethod is initialized or not. If not, then it returns false; otherwise it returns true. Also, write the logic for Execute method. Here, we simply initialize the _execute method because we make this RelayCommand class to be generalized.
- public boolCanExecute(object parameter) {
- if (_canexecuteMethod != null) {
- return true;
- } else {
- return false;
- }
- }
- public void Execute(object parameter) {
- _execteMethod(parameter);
- }
Now, let's define “CanExecuteChanged” the event logic, which will run the canExecute method continuously.
- public event EventHandlerCanExecuteChanged
- {
- add { CommandManager.RequerySuggested += value; }
- remove { CommandManager.RequerySuggested -= value; }
- }
Full RelayCommand class code
- public class RelayCommand: ICommand
- Action < object > _execteMethod;
- Func <object, bool> _canexecuteMethod;
- public RelayCommand(Action < object > execteMethod, Func <object , bool> canexecuteMethod) {
- _execteMethod = execteMethod;
- _canexecuteMethod = canexecuteMethod;
- }
- public boolCanExecute(object parameter) {
- if (_canexecuteMethod != null) {
- return _canexecutemethod(parameter);
- } else {
- return false;
- }
- }
- public event EventHandlerCanExecuteChanged {
- add {
- CommandManager.RequerySuggested += value;
- }
- remove {
- CommandManager.RequerySuggested -= value;
- }
- }
- public void Execute(object parameter) {
- _execteMethod(parameter);
- }
- }


Robinson JoosaPosted Jul 11, 2020, 2:15 PM
Simple and nice description
Sourav Kumar DasPosted Nov 11, 2019, 4:28 AM
Nice Article.
Dinesh GabhanePosted Nov 8, 2019, 12:34 AM
Nice Article. Thank you
Prasanna MuraliPosted Oct 9, 2016, 10:18 AM
Nice post..
ketan borsdiyaPosted Oct 6, 2016, 12:36 AM
Good..