This project is read-only.
When writing MVVM, it's forbidden to use click handlers (or other UI events) in your view-model. But then should you react to events?

1. Start with creating a command like you are used to using MVVM. This command will be executed when the event occurs.
2. Add a reference to System.Windows.Interactivity.dll (ships with Catel). If you have used NuGet to add a reference, it is automatically included for you.
3. Add the following namespace definitions to your view declaration:


4. Use the following code to convert an event to a command:

    <i:EventTrigger EventName="[YourEvent]">
        <catel:EventToCommand Command="{Binding [YourCommand]}" DisableAssociatedObjectOnCannotExecute="False" />

An example for a ListBox double click:

<ListBox ItemsSource="{Binding PersonCollection}" SelectedItem="{Binding SelectedPerson}">
        <i:EventTrigger EventName="MouseDoubleClick">
            <catel:EventToCommand Command="{Binding Edit}" DisableAssociatedObjectOnCannotExecute="False" />
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding FirstName}" />
                <Label Content="{Binding MiddleName}" />
                <Label Content="{Binding LastName}" />

Last edited Aug 4, 2011 at 7:12 AM by GeertvanHorrik, version 2


No comments yet.