Continuing down the path to MVVM, I have come to button commands. After quite a bit of trial and error I finally have a working example of a Button_Click
command using ICommand
.
My issue is that now I have a generic event made, I can't get which button was clicked to apply some logic to it. In my example, I have not used anything where I could get the Sender
information. Usually something like this below using RoutedEventArgs
:
Button button = (Button)sender;
So this is what I have so far.
The ICommand
class:
public class CommandHandler : ICommand
{
private Action _action;
private bool _canExecute;
public CommandHandler(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}
And the code to make the action:
private ICommand _clickCommand;
public ICommand ClickCommand => _clickCommand ?? (_clickCommand = new CommandHandler(MyAction, _canExecute));
public ViewModelBase()
{
_canExecute = true;
}
public void MyAction()
{
//Apply logic here to differentiate each button
}
And the XAML,
<Button Command="{Binding ClickCommand}" Style="{StaticResource RedButtonStyle}">MyButton</Button>
How would I go about identifying which button is being clicked when binding the same command to other buttons?
You probably shouldn't, but if you want to, you can use CommandParameter=""
You should just use 2 ICommands though.
XAML:
<Button Command="{Binding ClickCommandEvent}" CommandParameter="Jack"/>
ViewModel:
public RelayCommand ClickCommandEvent { get; set; }
public SomeClass()
{
ClickCommandEvent = new RelayCommand(ClickExecute);
}
public void ClickExecute(object param)
{
System.Diagnostics.Debug.WriteLine($"Clicked: {param as string}");
string name = param as string;
if (name == "Jack")
HighFive();
}
and your RelayCommand class would be this boiler plate:
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion
#region Constructors
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With