Double Click Command Behavior
I recently posted how to bind an ICommand on a ViewModel to an Input binding.
I thought I would also add a post sharing an attached dependency property I use to binding ‘double click’ to an ICommand.
The xaml to use the behavior looks like : (naturally where Behaviors is my xml namespace, etc)
<ListBox Style="{StaticResource ListBoxStyle}"
SelectedItem="{Binding SelectedItem}"
Behaviors:DoubleClickCommandBehavior.DoubleClickCommand="{Binding EditCommand}"
ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource Template}">
</ListBox>
What follows is the code. Enjoy! – JB
1: /// <summary>
2: /// Double Click Command Behavior.
3: /// Is a attached dependency property where you attach a command to execute upon the dependency object
4: /// (control) being 'double clicked'
5: /// </summary>
6: /// <remarks>created by Jonathan Birkholz 7/21/08</remarks>
7: public static class DoubleClickCommandBehavior
8: {
9: #region Double Click Command Property
10:
11: public static readonly DependencyProperty DoubleClickCommandProperty =
12: DependencyProperty.RegisterAttached("DoubleClickCommand",
13: typeof(ICommand), typeof(DoubleClickCommandBehavior),
14: new PropertyMetadata(null, DoubleClickCommandChanged));
15:
16: public static ICommand GetDoubleClickCommand(DependencyObject obj)
17: {
18: return (ICommand)obj.GetValue(DoubleClickCommandProperty);
19: }
20:
21: public static void SetDoubleClickCommand(DependencyObject obj, ICommand value)
22: {
23: obj.SetValue(DoubleClickCommandProperty, value);
24: }
25:
26: #endregion
27:
28: #region Double Click Command Parameter Property
29:
30: public static readonly DependencyProperty DoubleClickCommandParameterProperty =
31: DependencyProperty.RegisterAttached("DoubleClickCommandParameter",
32: typeof(object), typeof(DoubleClickCommandBehavior),
33: new PropertyMetadata(null));
34:
35: public static object GetDoubleClickCommandParameter(DependencyObject obj)
36: {
37: return (object)obj.GetValue(DoubleClickCommandParameterProperty);
38: }
39:
40: public static void SetDoubleClickCommandParameter(DependencyObject obj, object value)
41: {
42: obj.SetValue(DoubleClickCommandParameterProperty, value);
43: }
44:
45: #endregion
46:
47: #region Double Click Command Changed
48:
49: private static void DoubleClickCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
50: {
51: Selector selector = obj as Selector;
52:
53: if (selector != null)
54: selector.PreviewMouseLeftButtonDown += HandlePreviewMouseLeftButtonDown;
55: }
56:
57: private static void HandlePreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseEventArgs)
58: {
59: if (mouseEventArgs.ClickCount == 2)
60: {
61: DependencyObject depObj = sender as DependencyObject;
62:
63: if (depObj != null)
64: {
65: Selector selector = depObj as Selector;
66:
67: if (selector != null)
68: {
69: if (selector.SelectedItem != null)
70: {
71: ICommand command = GetDoubleClickCommand(depObj);
72: object commandParameter = GetDoubleClickCommandParameter(depObj);
73:
74: command.Execute(commandParameter);
75: }
76: }
77:
78: }
79: }
80: }
81:
82: #endregion
83: }

