Visakh V A
Visakh V A

Reputation: 320

Bind event in MVVM and pass event arguments as command parameter

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

Upvotes: 4

Views: 8960

Answers (3)

Ankit
Ankit

Reputation: 672

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.

Upvotes: 6

Peregrine
Peregrine

Reputation: 4546

Take a look at the MVVM-Light framework. Their implementation of EventToCommand includes a PassEventArgsToCommand option.

See this question, and this old blog post from Laurent Bugnion for more details.

Upvotes: 1

user5447154
user5447154

Reputation:

Should using CommandParameter ..It's all :)

<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="{Binding AppointmentEditing}" CommandParameter="YourParameters" />
            </intr:EventTrigger>
        </intr:Interaction.Triggers>
    </Canvas>

Upvotes: 1

Related Questions