I wanted to capture the double click event, which is actually the MouseLeftButtonDown with the e.ClickCount ==2.
Since I transitioned to a new WPF project, handed off the Silverlight work, I have been trying to keep the same MVVM pattern. I wanted to get the double click off of my TreeViewItem so the child node could invoke the ViewModel to execute a Command.
Here is the behavior I wrote to handle this simple task.
Snippet
using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Controls; namespace Seekford.Behaviors { public static class MouseDoubleClickBehavior { /// <summary> /// Hooks up a weak event against the source Selectors MouseDoubleClick /// if the Selector has asked for the HandleDoubleClick to be handled ///� /// If the source Selector has expressed an interest in not having its /// MouseDoubleClick handled the internal reference /// </summary> private static void OnHandleDoubleClickCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { FrameworkElement ele = d as FrameworkElement; if (ele != null) { ele.MouseLeftButtonDown -= OnMouseDoubleClick; if (e.NewValue != null) { ele.MouseLeftButtonDown += OnMouseDoubleClick; } } } /// <summary> /// TheCommandToRun : The actual ICommand to run /// </summary> public static readonly DependencyProperty DoubleClickCommandProperty = DependencyProperty.RegisterAttached("DoubleClickCommand", typeof(ICommand), typeof(MouseDoubleClickBehavior), new FrameworkPropertyMetadata((ICommand)null, new PropertyChangedCallback(OnHandleDoubleClickCommandChanged))); /// <summary> /// Gets the TheCommandToRun property. � /// </summary> public static ICommand GetDoubleClickCommand(DependencyObject d) { return (ICommand)d.GetValue(DoubleClickCommandProperty); } /// <summary> /// Sets the TheCommandToRun property. � /// </summary> public static void SetDoubleClickCommand(DependencyObject d, ICommand value) { d.SetValue(DoubleClickCommandProperty, value); } #region Handle the event /// <summary> /// Invoke the command we tagged. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnMouseDoubleClick(object sender, MouseButtonEventArgs e) { //check for double clicks. if (e.ClickCount != 2) return; FrameworkElement ele = sender as FrameworkElement; DependencyObject originalSender = e.OriginalSource as DependencyObject; if (ele == null || originalSender == null) return; ICommand command = (ICommand)(sender as DependencyObject).GetValue(DoubleClickCommandProperty); if (command != null) { if (command.CanExecute(null)) command.Execute(null); } } #endregion } }
Hopefully this helps you.
Oh, how to use. Almost forgot:
Snippet
xmlns:infraBehaviors="clr-namespace:Seekford.Behaviors;assembly=Seekford"
Snippet
<Grid infraBehaviors:MouseDoubleClickBehavior.DoubleClickCommand="{Binding MyCommand}" HorizontalAlignment="Stretch" > <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> � </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <TextBlock Text="{Binding Name}" TextWrapping="Wrap" FontWeight="Bold" Grid.Column="0" /> </Grid>
Of course, make your own namespace and correct accordingly. But you already knew that.
Happy Coding!