Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert the RoutedEvent to the command for MVVM pattern in WPF

Tags:

wpf

How to convert the Checkbox.checked routed event to the command for MVVM pattern?

Reference-Consider the button click routed event, When we assign the command for button then button.click event is considered as command. It executes respective that command.

I googled for it, i got the solution with EventBehaviourFactory. Is it possible without using the EventBehaiourFactory?

like image 654
rohanw Avatar asked Sep 10 '25 20:09

rohanw


1 Answers

It is a common requirement in WPF. The most widely used solution is using Interactivity from the Blend SDK:

Add this xmlns namespace to your xaml:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

Then you can do:

<CheckBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Checked">
            <i:InvokeCommandAction Command="{Binding MyCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</CheckBox>

Of course, you need to add System.Windows.Interactivity.dll to your project, located (for me at least) at: C:\Program Files (x86)\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\WPF\System.Windows.Interactivity.dll

like image 85
Louis Kottmann Avatar answered Sep 13 '25 13:09

Louis Kottmann