Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

How to support example data with view models?

 To find out how to create design time data, see the designers topic.

How to use events with MVVM?

...

 

...

When writing MVVM, it's "forbidden" (read: not a best practice) 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:

    Code Block
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:catel="http://catel.codeplex.com"
  4. Use the following code to convert an event to a command:

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

An example for a ListBox double click:

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

Silverlight throws error about type that cannot be created?

...

If you are using type creation via xaml this way:

Code Block
<i:Interaction.Behaviors>
  <catel:WindowBehavior ViewModelType="viewmodels:DemoWindowViewModel" Save="okButton.Click" Cancel="cancelButton.Click" />
</i:Interaction.Behaviors>

It might be possible that Silverlight throws an exception with these details:

Code Block
{System.Windows.Markup.XamlParseException: Failed to create a 'System.Type' from the text 'ViewModels:DemoWindowViewModel'. [Line: 13 Position: 67]
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at Catel.Examples.AdvancedDemo.Views.LogicInBehavior.DemoWindow.InitializeComponent()
at Catel.Examples.AdvancedDemo.Views.LogicInBehavior.DemoWindow..ctor()}

This happens in Silverlight 4 when the Silverlight 5 (beta) tools are not installed. To resolve this issue, install the Silverlight 5 (beta) tools.

How can I add the MVVM behaviors via code (programmatically)?

...