Hi,
EventHandler CanExecuteChanged you can use to raise the CanExecuteChanged event to indicate that the return value of the CanExecute method has changed. Try following demo.
XAML:
<Window x:Class="WpfApp1.Window069"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp069"
mc:Ignorable="d"
Title="Window069" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<StackPanel>
<Button Content="Click to change enabled bext button" Command="{Binding Cmd1}"/>
<Button Content="disabled / enabled button" Command="{Binding Cmd2}"/>
</StackPanel>
</Window>
And classes:
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
namespace WpfApp069
{
public class ViewModel
{
private RelayCommand _cmd1;
public ICommand Cmd1
{
get
{
Debug.Print("Getter property Cmd1");
if (this._cmd1 == null) this._cmd1=new RelayCommand(CmdExec1, CanCmdExec1);
return this.cmd1;
}
}
private bool CanCmdExec1(object obj) => true;
private void CmdExec1(object obj)
{
this._enabled = !_enabled;
this._cmd2.RaiseCanExecuteChanged();
Debug.Print("CmdExec1");
}
private RelayCommand _cmd2;
public ICommand Cmd2
{
get
{
Debug.Print("Getter property Cmd2");
if (this._cmd2 == null) this._cmd2=new RelayCommand(CmdExec2, CanCmdExec2);
return this._cmd2;
}
}
private bool _enabled;
private bool CanCmdExec2(object obj) => _enabled;
private void CmdExec2(object obj) => Debug.Print("CmdExec2");
}
/// <summary>
/// A command whose sole purpose is to relay its functionality
/// to other objects by invoking delegates.
/// The default return value for the CanExecute method is 'true'.
/// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
/// <see cref="CanExecute"/> is expected to return a different value.
/// </summary>
public class RelayCommand : ICommand
{
#region Private members
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
private readonly Action<object> _execute;
/// <summary>
/// True if command is executing, false otherwise
/// </summary>
private readonly Predicate<object> _canExecute;
#endregion
/// <summary>
/// Initializes a new instance of <see cref="RelayCommand"/> that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<object> execute) : this(execute, canExecute: null) { }
/// <summary>
/// Initializes a new instance of <see cref="RelayCommand"/>.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null) throw new ArgumentNullException("execute");
this._execute = execute;
this._canExecute = canExecute;
}
/// <summary>
/// Raised when RaiseCanExecuteChanged is called.
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
/// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
/// </summary>
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
/// </param>
/// <returns>True if this command can be executed; otherwise, false.</returns>
public bool CanExecute(object parameter) => this._canExecute == null ? true : this._canExecute(parameter);
/// <summary>
/// Executes the <see cref="RelayCommand"/> on the current command target.
/// </summary>
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
/// </param>
public void Execute(object parameter) => this._execute(parameter);
/// <summary>
/// Method used to raise the <see cref="CanExecuteChanged"/> event
/// to indicate that the return value of the <see cref="CanExecute"/>
/// method has changed.
/// </summary>
public void RaiseCanExecuteChanged() => this.CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
Result: