I have a class called RelayCommand
that inherits the ICommand interface defined like this:
/// <summary>
/// Base command that executes an <see cref="Action"/>.
/// </summary>
public class RelayCommand : ICommand
{
#region Members
// The Action to run.
private Action mAction;
#endregion
#region Events
/// <summary>
/// Event fired when the value of <see cref="CanExecute(object)"/> changes.
/// </summary>
public event EventHandler CanExecuteChanged = (sender, e) => { };
#endregion
#region Constructor
/// <summary>
/// Default <see cref="RelayCommand"/> constructor.
/// </summary>
public RelayCommand(Action action)
{
mAction = action;
}
#endregion
#region Command Methods
/// <summary>
/// A <see cref="RelayCommand"/> can always be executed.
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
return true;
}
/// <summary>
/// Executes the <see cref="Action"/> of the command.
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
mAction();
}
#endregion
}
I use this class to execute commands throughout the application and sometimes I need to perform Action
s passing a parameter in the XAML. The problem is that this parameter passing is optional but I can't find a solution that fits with what I'm doing. For example, I have these three commands that basically do the same thing. I set the PriorityLevel
(enum
value) to an object through the UI and, depending on which control is clicked, I execute one of the following three methods:
/// <summary>
/// What to do when the priority of the task is set to <see cref="PriorityLevel.Low"/>
/// </summary>
private void OnSetLowPriorityCommand()
{
Priority = PriorityLevel.Low;
PriorityGridWidth = 10;
PriorityFlagOpacity = 0;
mIsPriorityChanged = true;
}
/// <summary>
/// What to do when the priority of the task is set to <see cref="PriorityLevel.Medium"/>
/// </summary>
private void OnSetMediumPriorityCommand()
{
Priority = PriorityLevel.Medium;
PriorityGridWidth = 10;
PriorityFlagOpacity = 0;
mIsPriorityChanged = true;
}
/// <summary>
/// What to do when the priority of the task is set to <see cref="PriorityLevel.High"/>
/// </summary>
private void OnSetHighPriorityCommand()
{
Priority = PriorityLevel.High;
PriorityGridWidth = 10;
PriorityFlagOpacity = 0;
mIsPriorityChanged = true;
}
As you can see, apart of the first line of the methods, the rest is the same, but I think that it would be better if I keep only one method called, for example, SetPriorityCommand
that, through a switch
or whatever, sets the right PriorityLevel
value. I then consume the Action
s like this:
SetLowPriorityCommand = new RelayCommand(OnSetLowPriorityCommand);
Anyways, in other cases I execute an Action
without the need of passing a parameter.
Finally, in the XAML I need to execute commands like this:
<Border.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="{Binding SetLowPriorityCommand}"
CommandParameter="{Binding Source={x:Static core:PriorityLevel.Low}}"/> <!-- NOT ALWAYS NECESSARY -->
</Border.InputBindings>
I found plenty of answers about this but none of them seemed to make the use of parameters optional.
How can I adjust the RelayCommand
class to fit my needs?
Thank you in advance for the help.