Bind event in MVVM and pass event arguments as command parameter

.net, c#, mvvm, wpf

Solution

You can do it by adding the DLL's:

- `System.Windows.Interactivitiy`

- `Microsoft.Expression.Interactions`

In your XAML:

Use the CallMethodAction class.

Use the `EventName` to call the event you want; then specify your `Method` name in the `MethodName`.

<Window>
    xmlns:wi="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">

    <wi:Interaction.Triggers>
        <wi:EventTrigger EventName="SelectionChanged">
            <ei:CallMethodAction
                TargetObject="{Binding}"
                MethodName="ShowCustomer"/>
        </wi:EventTrigger>
    </wi:Interaction.Triggers>
</Window>

In your ViewModel Code:

public void ShowCustomer()
//The method must be public & can take 0 parameters or 2 parameters i.e.
//object sender & EventArgs args
{
    // Do something.
}

P.S: This is a real late response to the question but I hope it helps you.

Problem

I wanted to bind an event with ViewModel. I used clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity and i used trigger for the same ``` <Canvas Grid.Row="2" Grid.Column="2" x:Name="InteractiveCanvas" Style="{StaticResource canvasChartStyle}" ClipToBounds="True" > <intr:Interaction.Triggers> <intr:EventTrigger EventName="MouseEnter"> <intr:InvokeCommandAction Command="AppointmentEditing" /> </intr:EventTrigger> </intr:Interaction.Triggers> </Canvas> ``` but I need event arguments to be used. Here am not able to get the same. In wpf any possiblity is there to bind event and get event arguments ? With out ussing MVVM lite or PRISM. I just want to get the event arguments

Original source

Related problems