35

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.

David Veeneman
  • 18,912
  • 32
  • 122
  • 187

9 Answers9

44

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.

Community
  • 1
  • 1
marapet
  • 54,856
  • 12
  • 170
  • 184
40

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.

David Veeneman
  • 18,912
  • 32
  • 122
  • 187
  • 1
    This is really handy. I wasn't using data templates for my list (just wanted to display some text), and this saves me having to wrap my text up in a templated control – Frames Catherine White May 09 '12 at 13:09
  • 13
    It should be noted that the `ListBox.MouseDoubleClicked` event fires when the mouse is double clicked anywhere inside the control. IE: This includes double clicks that aren't on any `ListBoxItem`. – Chris Kerekes Mar 26 '13 at 20:14
  • 1
    @ChrisKerekes You're right. As reference, I think this can be solved by using `if (listBox.SelectedIndex == -1) return;`. – Marlos Aug 20 '13 at 17:23
  • 3
    @Marlos: Nope. The last selected item remains selected if you click on empty space in a listbox. – John Reynolds Jan 27 '15 at 11:37
  • 6
    And even fires when you double click the scrollbar – Abdul Saleem Apr 14 '15 at 09:01
  • 1
    You can filter out unwanted double-clicks by checking `e.Source` or `e.OriginalSource` for being a wanted element (in this case - ListBoxItem). – Astrogator Jul 06 '15 at 15:17
  • @Astrogator I don't think that you can rely on the double clicked item being a `ListBoxItem`. Depending on the datatemplate being used, `e.OriginalSource` may be a `Border` or some other control. – Zack Aug 03 '15 at 19:32
  • 1
    @Zack, that's why i said: **you can filter out unwanted .. by checking [either source] for being a wanted element (in this case - ListBoxItem)** – Astrogator Aug 05 '15 at 20:30
  • @Astrogator I guess what I was trying to say was that even if the user clicked on what you might consider the `ListBoxItem`, that the `DataTemplate` for the item might report the `OriginalSource` or `Source` as a `Border` or some other control in the *visual* layout. – Zack Aug 06 '15 at 13:55
  • @Zack, I know what you're saying. But you keep missing my point - that's exactly why i advised: **filter unwanted source elements** (i.e. check if they are what you want). When/if you do then you're guaranteed that the clicked source was [in our case] `ListBoxItem`. – Astrogator Aug 06 '15 at 14:35
  • @Astrogator By "filter" do you mean something like this answer http://stackoverflow.com/a/929960/1804496 where you loop through the visual tree? If you are simply checking the `type` of the source elements, then you will *never* get a `ListBoxItem`, since it will not be the lowest level UI element that actually gets clicked. – Zack Aug 06 '15 at 15:47
  • @Zack, By "filter out" i mean "filter out" - i.e. check-n-skip by whatever means necessary (i left technical details out, since it was a mere comment). In WPF for scenarios like that you cannot rely on just doing an *if*-check, you **must** walk the visual tree. Yes, the link you found is exactly the kind of filtering i referred to. You have to agree though, `e.OriginalSource` *is* the input for it. – Astrogator Aug 07 '15 at 18:05
16

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

gls123
  • 5,467
  • 2
  • 28
  • 28
  • 2
    Note with this solution, that if you click within the ListBoxItem but outside the text block (in case of padding/margin), the event will not fire. This may or may not be desirable. – Clay Feb 25 '14 at 17:18
  • ^ To get around this, just give the TextBlock a `Background`, like `Transparent`, and you can then double click anywhere within the bounds of the `TextBlock`. – Mike Eason May 22 '15 at 09:20
  • Thanks, this is a much better solution to me than the build-in MouseDoubleClick-event of ListBox, since it limits the double click to the actual items without extended hacking. – germanSharper Sep 05 '16 at 13:36
  • @MikeEason Background=Transparent didn't work for me as this doesn't make the textblock fill width. – Bogdan Verbenets Dec 05 '16 at 00:49
6

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.

Ben Von Handorf
  • 2,326
  • 1
  • 15
  • 17
5

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}"
         />
user3313608
  • 241
  • 3
  • 4
4

According to an MSDN article you can add a handler to every item in the listbox as follows:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

In the code behind you would then handle the double click. You could can then get access to the DataContext via sender.DataContext in the handler. Since double clicking selects an item you could also use the SelectedItem property or similar properties on the ListBox.

Personally I'd prefer a Behavior solution that allows redirection to a command, but I'm not sure how to implement that.

Wouter
  • 2,170
  • 1
  • 28
  • 58
1

The ListBox has a double click event now.

0

On the double-click event use:

if (e.OriginalSource.GetType().ToString() == "System.Windows.Controls.TextBlock")
     DoubleClickDoneOnItem(); 
Grunwaldt
  • 41
  • 8
  • You could also return `System.Windows.Controls.TextBlock` by double clicking on the column header if the list has one so I'm not sure this would work without the possibility of causing an exception. – Fütemire Feb 03 '20 at 21:52
0

None of the current answers worked in my case since I am using SelectionMode="Multiple", so I just want to share with you the cleanest way I found to implement this feature.

First, I created a handler for a ListBoxItem included:

public void listBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListBoxItem clickedItem = ((ListBoxItem)sender);
            
    for (int i = 0; i < listBox.Items.Count; i++)
    {
        if (listBoxECIRatioSizeAutoSelect.Items[i] == clickedItem)
        {
            doStuff(i);
            break;
        }
    }
}

Then, whenever a ListBoxItem is added to the ListBox, the Handler is added to the ListBoxItem:

private void addItemToListBox(string strItem)
{
    ListBoxItem newItem = new ListBoxItem();
    newItem.Content = strItem;
    newItem.MouseDoubleClick += listBoxItem_MouseDoubleClick;
    listBox.Items.Add(newItem);
}