Sam
Sam

Reputation: 75

WPF RelayCommand not firing on button click

I'm inexperienced with both WPF and MVVM so i'm most likeley missing something but when I click my button the command isn't firing. I also have some menu controls on my page that i've setup the exact same way and when I click those, their commands work as expected.

I've tried attaching a click event handler to make sure the button is definitely being clicked which it is. I've also tried attaching a different command that works on my menu control which didn't work on the button.

<Button Grid.Row="1" Content="Add Note" 
                                        Command="{Binding InsertNoteCommand}"/>
public ICommand InsertNoteCommand { get; }

public MainViewModel()
{
    InsertNoteCommand = new RelayCommand(InsertNote);
}

private void InsertNote()
{
    Console.WriteLine("Note Inserted!");
}

I should also mention that i'm using MVVM Light

Upvotes: 0

Views: 1267

Answers (1)

Sam
Sam

Reputation: 75

The debugging information is very useful to know but in the end I solved the problem by pointing the binding to the data context.

<Button x:Name="AddNewNoteBtn" Grid.Row="1" Content="Add Note" 
                                        Command="{Binding Path=DataContext.InsertNoteCommand,  ElementName=_window}"/>

If anybody has comments on how I can improve this I would really appreciate it. Thanks!

Upvotes: 1

Related Questions