Bind an event in MVVM and pass event arguments as a parameter to the command

I wanted to associate an event with a ViewModel.

I used

CLRs: System.Windows.Interactivity; assembly = System.Windows.Interactivity

and I used a 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 I can not get the same.

In wpf, is there any way to bind events and get event arguments? Without using MVVM lite or PRISM.

I just want to get event arguments

+4
source share
3 answers

You can do this by adding a DLL:

  • System.Windows.Interactivitiy
  • Microsoft.Expression.Interactions

In your XAML:

Use the CallMethodAction class.

EventName ; Method 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>

ViewModel:

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: , , .

+3

CommandParameter..It :)

<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>
+1

Take a look at the MVVM-Light structure. Their implementation of EventToCommand includes the PassEventArgsToCommand parameter.

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

0
source

Source: https://habr.com/ru/post/1658073/


All Articles