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.

             xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"




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

<UserControl.Resources>
        <Local:vmA01S020 x:Name="dvm" />
</UserControl.Resources>

Now tell your Grid that it is the DataContext.

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

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

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

    </i:Interaction.Triggers>

ViewModel:

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

        }

Comments

Popular Posts