Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM Pattern - How can I trigger an ICommand or CommandReference in the code behind

Tags:

mvvm

wpf

I have been looking for this on Stack Overflow, but couldnt find an answer to this yet so I hope this isnt a duplicate...

I have an app using the MVVM pattern, I like to keep things clean, but sometimes a little code behind seems cleaner than the XAML workaround.

I want to know if it is possible to trigger a command from the codebehind.

I have set up a command reference in my view already (see below)

<local:CommandReference 
                 x:Key="CommandReferencePreviewReportsCommand" 
                 Command="{Binding PreviewReportsCommand}" />

What I want to do is be able to trigger the command reference or the ICommand from the c# code behind of the view. Does anyone know how to do this?

like image 242
Mark Pearl Avatar asked Jan 19 '10 08:01

Mark Pearl


1 Answers

You can just invoke the command from the code-behind:

var parameter = null; // can also be any object you'd like to use as a parameter
this.PreviewReportsCommand.Execute(parameter);
like image 118
Mark Seemann Avatar answered Oct 05 '22 08:10

Mark Seemann