Skip to content

Commit

Permalink
Improve BindableCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Dec 23, 2024
1 parent 89b359a commit fec203e
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions Src/Denovo/MVVM/BindableCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ public BindableCommand(Action executeMethod) : this(executeMethod, null)
{
}

public BindableCommand(Action executeMethod, Func<bool> canExecuteMethod)
public BindableCommand(Action executeMethod, Func<bool>? canExecuteMethod)
{
ExecuteMethod = executeMethod;
CanExecuteMethod = canExecuteMethod;
}


private readonly Action ExecuteMethod;
private readonly Func<bool> CanExecuteMethod;
private readonly Func<bool>? CanExecuteMethod;

public event EventHandler CanExecuteChanged = delegate { };
public event EventHandler? CanExecuteChanged;


public void RaiseCanExecuteChanged() => CanExecuteChanged(this, EventArgs.Empty);
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

public bool CanExecute(object parameter) => CanExecuteMethod is null || CanExecuteMethod();
public bool CanExecute(object? parameter) => CanExecuteMethod is null || CanExecuteMethod();

public void Execute(object parameter) => ExecuteMethod?.Invoke();
public void Execute(object? parameter) => ExecuteMethod?.Invoke();
}


Expand All @@ -41,23 +41,23 @@ public BindableCommand(Action<T> executeMethod) : this(executeMethod, null)
{
}

public BindableCommand(Action<T> executeMethod, Func<bool> canExecuteMethod)
public BindableCommand(Action<T> executeMethod, Func<bool>? canExecuteMethod)
{
ExecuteMethod = executeMethod;
CanExecuteMethod = canExecuteMethod;
}


private readonly Action<T> ExecuteMethod;
private readonly Func<bool> CanExecuteMethod;
private readonly Func<bool>? CanExecuteMethod;

public event EventHandler CanExecuteChanged = delegate { };
public event EventHandler? CanExecuteChanged;


public void RaiseCanExecuteChanged() => CanExecuteChanged(this, EventArgs.Empty);
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

public bool CanExecute(object parameter) => !(CanExecuteMethod is null) && CanExecuteMethod();
public bool CanExecute(object? parameter) => CanExecuteMethod is null || CanExecuteMethod();

public void Execute(object parameter) => ExecuteMethod?.Invoke((T)parameter);
public void Execute(object? parameter) => ExecuteMethod?.Invoke((T)parameter);
}
}

0 comments on commit fec203e

Please sign in to comment.