Binding Events to Methods in ViewModel instead of using RelayCommands

Instead of binding your event with command and then on that command, calling your method you can do this directly. Just import a namespace and you are good to go.

Add References:

Add reference of  Microsoft.Expression.Interactions to you silverlight project.

Includes these two namespaces in your xaml file.


Your XAML:

You need to set your main Grid's DataContext your view model. You can do it either in .cs file or in Xaml.
I do it in xaml, first add your view model in resources

        <Local:vmA01S020 x:Name="dvm" />

Now tell your Grid that it is the DataContext.

<Grid x:Name="LayoutRoot"
          DataContext="{Binding Source={StaticResource dvm}}">

Write your event along with its method to be called upon like below:

        <i:EventTrigger EventName="SizeChanged">
            <ei:CallMethodAction MethodName="WindowSizeChanged"
                                 TargetObject="{Binding}" />



       public void WindowSizeChanged(object sender, SizeChangedEventArgs e)
            //     Whatever you wanna do, write it here


Popular Posts