1

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); } 

1 Answer 1

1

I figured it out. My DataContext binding wasn't working. IChanged it to:

xmlns:vm="clr-namespace:ProgramName.ViewModel" <Window.DataContext> <vm:Commands/> </Window.DataContext> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.