I'm trying to bind my own command to a button but nothing I try is working.
I got the window's DataContext bound via XAML but when I try to bind to my command IntelliSense is not seeing it, and the command is not executed. I tried to bind via code-behind but also met with the same result.
The window's Binding looks like this.
DataContext="{Binding Source={StaticResource mainViewModelLocator}, Path=Commands}" mainViewModelLocator passes a new instance of the Commands class.
The Commands class :
public ICommand GradeCommand { get; set; } public Commands() { LoadCommands(); } private void LoadCommands() { GradeCommand = new CustomCommand(GradeClick, CanGradeClick); } private void GradeClick(object obj) { MessageBox.Show("Test"); } private bool CanGradeClick(object obj) { return true; } and ICommand:
private Action<object> execute; private Predicate<object> canExecute; public CustomCommand(Action<object> execute, Predicate<object> canExecute) { this.execute = execute; this.canExecute = canExecute; } public bool CanExecute(object parameter) { bool b = canExecute == null ? true : canExecute(parameter); return b; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { execute(parameter); }