To answer your question: yes, there is.
You need to create a Command implementation of the ICommand class:
public class MyCommand : ICommand { Action<bool> _action; public MyCommand(Action<bool> action) { _action = action; } public bool CanExecute(object parameter) { return true; } public event System.EventHandler CanExecuteChanged; public void Execute(object parameter) { _action((bool)parameter); } }
then in your ViewModel create a command:
private MyCommand simpleCommand; public MyCommand SimpleCommand { get { return simpleCommand; } set { simpleCommand = value; } } public MainViewModel() { SimpleCommand = new MyCommand(new Action<bool>(DoSomething)); } public void DoSomething(bool isChecked) {
And bind the Checkbox command to it, and CommandParameter
<CheckBox Name="checkBox1" Command="{Binding Path=SimpleCommand}" CommandParameter="{Binding ElementName=checkBox1, Path=IsChecked}" />
But this is a little exaggerated. You should probably create the corresponding bool property in the ViewModel, bind to it and call the required code inside the accessor.
source share