37

Can anyone tell me what the actual syntax is for EventToCommand class. From what I believe is that EventToCommand class works with Silverlight / WPF and WP7, hence I think its a better choice to go down.

From what I believe, I can add any click event and get it forced into my ViewModel, but I am having an issue in finding the best way to do this.

I know you can add it without Blend, but are there snippets available?

Or is there an easier way to add it via VS 2010? Any help or if anyone knows of a good tutorial on this would be great.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Martin
  • 23,844
  • 55
  • 201
  • 327
  • 1
    I've created [a behavior](http://stackoverflow.com/a/16317999/385995) that does not require MVVMLight. It does require System.Windows.Interactivity, but so does the accepted answer. – Mike Fuchs Sep 25 '14 at 17:28
  • Here's a post that talks about [all you need to know about EventToCommand](http://geekswithblogs.net/lbugnion/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx) from the creator of MVVMLight ;) – AbdouMoumen May 03 '11 at 14:56

3 Answers3

84

Suppose you use .NetFramework4:

First add namespace:

xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"

Syntax for the Loaded event.

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <cmd:EventToCommand Command="{Binding Mode=OneWay, Path=LoadedCommand}"
                            PassEventArgsToCommand="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>
Derek Beattie
  • 9,429
  • 4
  • 30
  • 44
5

I updated my project and it looks like they moved the command to:

xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
Michael Shaffer
  • 374
  • 2
  • 16
  • 1
    How does this differ from http://stackoverflow.com/a/5869889/11635 - ah I see, it was a late edit. This should really have been a comment on the other answer in that instance – Ruben Bartelink Jun 14 '16 at 10:24
5

0) if you dont't know WPF and MVVM, then read Josh Smith article about WPF and MVVM pattern https://msdn.microsoft.com/en-us/magazine/dd419663.aspx

1) In your project add package (through NuGet) MvvmLightLibs

2) add reference to System.Windows.Interactivity

3) In "View" XAML add:

a)

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"

b)

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Closing">
      <command:EventToCommand Command="{Binding OnClosingCommand}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Window>

4) In ViewModel add necessary property

public ICommand OnClosingCommand
{
  get
  {
    return new RelayCommand(() => SomeMethod());
  }
}

P.S. In your View should be specified DataContext (XAML)

  <Window.DataContext>
    <vm:MainWindowViewModel/>
  </Window.DataContext>

It is work. I myself just learned.

Stepagrus
  • 1,189
  • 1
  • 12
  • 19
  • I am traying to implement ItemTapped in a listview…… but I dont know where to put yoour code, can you helpme? – KillemAll Jan 24 '19 at 14:26