A nice approach for this is to use an attached behaviour. The behaviour itself can hook the event you need and fire the appropriate command with the parameters you want. It's essentially the same code, but it just remove it from your code behind and lets you express your intent purely in XAML.
There are several example behaviours on CodePlex, or a here's a basic sample that executes a command:
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
/// <summary>
/// Trigger action to execute an ICommand command
/// </summary>
public class ExecuteCommand : TriggerAction<FrameworkElement>
{
#region Dependency Properties
/// <summary>
/// Command parameter
/// </summary>
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommand), new UIPropertyMetadata(null, OnCommandParameterChanged));
/// <summary>
/// Command to be executed
/// </summary>
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExecuteCommand), new UIPropertyMetadata(null, OnCommandChanged));
#region Public Properties
/// <summary>
/// Gets or sets the command
/// </summary>
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}
/// <summary>
/// Gets or sets the command parameter
/// </summary>
public object CommandParameter
{
get
{
return (object)this.GetValue(CommandParameterProperty);
}
set
{
this.SetValue(CommandParameterProperty, value);
}
}
#endregion
/// <summary>
/// Executes the command if it is not null and is able to execute
/// </summary>
/// <param name="parameter">This argument not used</param>
protected override void Invoke(object parameter)
{
if (this.Command != null && this.Command.CanExecute(this.CommandParameter))
{
this.Command.Execute(this.CommandParameter);
}
}
/// <summary>
/// Called on command change
/// </summary>
/// <param name="oldValue">old ICommand instance</param>
/// <param name="newValue">new ICommand instance</param>
protected virtual void OnCommandChanged(ICommand oldValue, ICommand newValue)
{
}
/// <summary>
/// Called on command parameter change
/// </summary>
/// <param name="oldValue">old ICommand instance</param>
/// <param name="newValue">new ICommand instance</param>
protected virtual void OnCommandParameterChanged(object oldValue, object newValue)
{
}
/// <summary>
/// Called on command parameter change
/// </summary>
/// <param name="o">Dependency object</param>
/// <param name="e">Dependency property</param>
private static void OnCommandParameterChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ExecuteCommand invokeCommand = o as ExecuteCommand;
if (invokeCommand != null)
{
invokeCommand.OnCommandParameterChanged((object)e.OldValue, (object)e.NewValue);
}
}
/// <summary>
/// Called on command change
/// </summary>
/// <param name="o">Dependency object</param>
/// <param name="e">Dependency property</param>
private static void OnCommandChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ExecuteCommand invokeCommand = o as ExecuteCommand;
if (invokeCommand != null)
{
invokeCommand.OnCommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue);
}
}
#endregion
}
}
You can then use the System.Windows.Interactivity namespace (the assembly is included with Blend 3) to hook the event and fire the command as follows:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<triggers:ExecuteCommand Command="{Binding MyCommand}" CommandParameter="MyParameter" />
</i:EventTrigger>
</i:Interaction.Triggers>
For more complicated events with multiple parameters you may need to create a specific Behaviour, rather than using a generic one like the example above. I generally prefer to create my own type to store parameters, and map to it, rather than having a specific EventArgs requirement in my ViewModel.
*One thing I should add is that I'm definately not a "0 code in the Code Behind" kind of guy, and I think enforcing this religiously is missing the point of MVVM somewhat. As long as the code behind contains no logic, and therefore nothing that really need to be under test, then I can live with some small pieces of code behind for "bridging the gap" between View and ViewModel. This is also sometimes necessary if you have a "smart view", as I generally call it, such as a browser control, or something you need to commmunicate with from your ViewModel. Some people would get pitchforks out for even suggesting such a thing, that's why I left this bit until last and answered your question first :-)*