简体   繁体   中英

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM