4

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

Visakh V A
  • 320
  • 3
  • 19

3 Answers3

6

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.

Ankit
  • 672
  • 6
  • 18
  • This works without the need to add another library just to get the event arguments. – hfann Jan 23 '17 at 21:36
  • @hfann: May i ask how? Because the Interaction.Triggers, EventTrigger & CallMethodAction are apart of the aforementioned assemblies. – Ankit Jan 24 '17 at 06:56
  • When I referred to a libaray, I meant MVVMLight or Prism. I'm already using System.Windows.Interactivityu DLL already. – hfann Jan 24 '17 at 16:11
  • Which effectively means one would require to use the DLL's mentioned above. – Ankit Jan 25 '17 at 11:20
1

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

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.

Community
  • 1
  • 1
Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • I cant use any framework here, whether any pure WPF idea could you suggest ? – Visakh V A Oct 19 '16 at 08:46
  • What's wrong with using a framework? Why do you want to reinvent the wheel? The best I can suggest is to download the MVVMLight source code and copy the relevant class (EventToCommand.cs) into your own library - https://mvvmlight.codeplex.com/SourceControl/latest – Peregrine Oct 19 '16 at 09:07
  • @VisakhVA yes using PassEventArgsToCommand="True", you must chage a litle be your command then. –  Oct 19 '16 at 09:47