Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wanted to solve this without needing to handle the listBoxItem double click event in the code-behind, and I didn't want to have to override the listBoxItem style (or define the style to override in the first place). I wanted to just fire a command when the listBox was doubleclicked.</p> <p>I created an attached property like so (the code is very specific, but you can generalise it as required):</p> <pre><code>public class ControlItemDoubleClick : DependencyObject { public ControlItemDoubleClick() { } public static readonly DependencyProperty ItemsDoubleClickProperty = DependencyProperty.RegisterAttached("ItemsDoubleClick", typeof(bool), typeof(Binding)); public static void SetItemsDoubleClick(ItemsControl element, bool value) { element.SetValue(ItemsDoubleClickProperty, value); if (value) { element.PreviewMouseDoubleClick += new MouseButtonEventHandler(element_PreviewMouseDoubleClick); } } static void element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { ItemsControl control = sender as ItemsControl; foreach (InputBinding b in control.InputBindings) { if (!(b is MouseBinding)) { continue; } if (b.Gesture != null &amp;&amp; b.Gesture is MouseGesture &amp;&amp; ((MouseGesture)b.Gesture).MouseAction == MouseAction.LeftDoubleClick &amp;&amp; b.Command.CanExecute(null)) { b.Command.Execute(null); e.Handled = true; } } } public static bool GetItemsDoubleClick(ItemsControl element) { return (bool)element.GetValue(ItemsDoubleClickProperty); } </code></pre> <p>}</p> <p>I then declare my ListBox with the attached property and my target command:</p> <pre><code>&lt;ListBox ItemsSource="{Binding SomeItems}" myStuff:ControlItemDoubleClick.ItemsDoubleClick="true"&gt; &lt;ListBox.InputBindings&gt; &lt;MouseBinding MouseAction="LeftDoubleClick" Command="MyCommand"/&gt; &lt;/ListBox.InputBindings&gt; &lt;/ListBox&gt; </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload