1

I'm using some application commands like New, Open, etc and I'd like to have their executed and CanExecute handlers in the viewmodel but I'm having trouble with it and I can't seem to find anything that solves this. I have a commandBinding like this and I have a NewCommand_Executed meghot in the viewmodel, which is the datacontext of my window.

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.New" Executed="{Binding NewCommand_Executed}" CanExecute="Default_CanExecute" />
...

However, I get a parse exception for this with an inner exception saying: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'. Can someone explain what I am missing or if there is a way to achieve what I'm trying?

Peter
  • 1,047
  • 2
  • 18
  • 32
  • I think your commands in code need to be of type `ICommand`, and it sounds like you have them as methods instead. – Rachel Apr 15 '15 at 18:12
  • Take a look here. The second implementation with delegate commands will allow you to use CanExecute. http://stackoverflow.com/questions/28448319/how-to-pass-argument-to-the-method-present-in-event-trigger-wpf/28469215#28469215 – AzzamAziz Apr 16 '15 at 01:19

1 Answers1

0

If you are using the MVVM pattern, you should consider adopting delegate commands in place of CommandBindings. You can find here a tutorial about MVVM. The section, which talks about Commands, can help you.

Il Vic
  • 5,576
  • 4
  • 26
  • 37