WPF ListBoxItem double-click?

The WPF ListBox doesn't have a DoubleClick event, at least not as far as I can tell. Is there a workaround for this issue that would let me double-click on an item to have an event handler do something with it? Thanks for your help.

Answers


You could always override the ListItem Control Template and handle the double click event inside the template, for example in an invisible border that contains the normal contents of the ListBox.

This article shows you how to use a ControlTemplate with a ListBoxItem. Beyond that, simply add the handler to the outermost element of your control template.

If you have Expression Blend, you can use it to extract the existing control template for you to modify so that you do not have to do as much work to ensure that the new list box behaves the same as the old.


It turns out there is a MouseDoubleClick event for the ListBox. I added this event to my ListBox and had the event handler process my task, copying the selected item to another ListBox. So, now whenever I double-click on an item, it gets copied.


It is possible to bind commands with parameters to ListBoxItems without using code-behind or attached behaviors, simply by using InputBindings with a MouseBinding, as shown before in this answer.

Example ListBox with MouseBinding for LeftDoubleClick:

<ListBox ItemsSource="{Binding MyDataSource}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding MySourceItemName}">
                <TextBlock.InputBindings>
                    <MouseBinding MouseAction="LeftDoubleClick"
                                  Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
                                  CommandParameter="{Binding MySourceItemId}" />
                </TextBlock.InputBindings>
            </TextBlock>
        </DataTemplate> 
    </ListBox.ItemTemplate>
</ListBox>

If the command is defined in the same DataContext as the ItemsSource of the ListBox, it can be bound by using the RelativeSource binding as included in the example.


If you are using databinding, then this problem is very easy to solve

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding ObjectName}"
           MouseDown="TextBlock_MouseDown"/>
    </DataTemplate>
</ListBox.ItemTemplate>    

Then in your code behind, check for a double click as follows

private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount>=2)
    { 
        ....
    }
}

If your double clicked item was not selected before the double click, then it wont appear selected in the event handler. Your logic within that handler needs to bear this in mind


I have used the above answer from David (which starts with 'It turns out there is a MouseDoubleClick event for the ListBox.') to generate a solution that is based on his approach but is implemented with an attached behavior.

I am not saying you shall not have any code behind because I know there are situations where it should not be avoided for any price. But, this is yet another example of how you can convert an event based solution into an MVVM compliant solution that works via Event to command binding conversion.

This my attached behavior code:

using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

/// <summary>
/// Class implements a <seealso cref="Selector"/> double click
/// to command binding attached behaviour.
/// </summary>
public class DoubleClickSelectorItem
{
  #region fields
  public static readonly DependencyProperty DoubleClickItemCommandProperty =
      DependencyProperty.RegisterAttached("DoubleClickItemCommand",
                                          typeof(ICommand),
                                          typeof(DoubleClickSelectorItem),
                                          new PropertyMetadata(null,
                                          DoubleClickSelectorItem.OnDoubleClickItemCommand));
  #endregion fields

  #region constructor
  /// <summary>
  /// Class constructor
  /// </summary>
  public DoubleClickSelectorItem()
  {

  }
  #endregion constructor

  #region properties
  #endregion properties

  #region methods
  #region attached dependency property methods
  public static ICommand GetDoubleClickItemCommand(DependencyObject obj)
  {
    return (ICommand)obj.GetValue(DoubleClickItemCommandProperty);
  }

  public static void SetDoubleClickItemCommand(DependencyObject obj, ICommand value)
  {
    obj.SetValue(DoubleClickItemCommandProperty, value);
  }
  #endregion attached dependency property methods

  private static void OnDoubleClickItemCommand(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
    var uiElement = d as Selector;

    // Remove the handler if it exist to avoid memory leaks
    if (uiElement != null)
      uiElement.MouseDoubleClick -= UIElement_MouseDoubleClick;

    var command = e.NewValue as ICommand;
    if (command != null)
    {
      // the property is attached so we attach the Drop event handler
      uiElement.MouseDoubleClick += UIElement_MouseDoubleClick;
    }
  }

  private static void UIElement_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  {
    var uiElement = sender as Selector;

    // Sanity check just in case this was somehow send by something else
    if (uiElement == null)
      return;

    // Is there a selected item that was double clicked?
    if (uiElement.SelectedIndex == -1)
      return;

    ICommand doubleclickCommand = DoubleClickSelectorItem.GetDoubleClickItemCommand(uiElement);

    // There may not be a command bound to this after all
    if (doubleclickCommand == null)
      return;

    // Check whether this attached behaviour is bound to a RoutedCommand
    if (doubleclickCommand is RoutedCommand)
    {
      // Execute the routed command
      (doubleclickCommand as RoutedCommand).Execute(uiElement.SelectedItem, uiElement);
    }
    else
    {
      // Execute the Command as bound delegate
      doubleclickCommand.Execute(uiElement.SelectedItem);
    }            
  }
  #endregion methods
}

Usage in XAML:

<ListBox ItemsSource="{Binding CurrentItems}"
         behav:DoubleClickSelectorItem.DoubleClickItemCommand="{Binding Path=NavigateDownCommand}"
         />

The ListBox has a double click event now.


On the double-click event use:

if (e.OriginalSource.GetType().ToString() == "System.Windows.Controls.TextBlock")
     DoubleClickDoneOnItem(); 

Need Your Help

Best Practice Using C# XSD typed-datasets In Enterprise Applications

c# xsd strongly-typed-dataset

I'm trying to understand what is the best practice using XSD tables generated from the Database scheme that I currently use.

PHP proc_open & exec timeout but cli works fine

php symfony wkhtmltopdf proc-open symfony-process

I'm currently using the Symfony Process component, which relies on the proc_open function of PHP. I need to launch a command to wkhtmltopdf, which has this form :