wpflistboxeventsetter

Is there a way to respond to double-clicking on a ListBoxItem in WPF?


I have a simple WPF application with a ListBox of items. For the simplicity of this example, I made it a list of strings, but in reality, it will be a list of some complex type. When an item itself in the listbox is double clicked, I want to respond to it. Apparently, there is no direct double click event on the ListBox for the item itself. Is there a simple way of responding to an item in the ListBox being double-clicked (not the listbox itself)?

Here is my xaml:

<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox>
            <ListBox.Items>
                <sys:String>Item1</sys:String>
                <sys:String>Item2</sys:String>
                <sys:String>Item3</sys:String>
                <sys:String>Item4</sys:String>
                <sys:String>Item5</sys:String>
            </ListBox.Items>
        </ListBox>
    </Grid>
</Window>

Solution

  • This can be easily achieved by creating an ItemContainerStyle and adding an EventSetter.

    Modify your XAML like so:

    <Window x:Class="WpfApplication12.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ListBox>
    
                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                        <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_DoubleClick" />
                    </Style>
                </ListBox.ItemContainerStyle>
    
                <ListBox.Items>
                    <sys:String>Item1</sys:String>
                    <sys:String>Item2</sys:String>
                    <sys:String>Item3</sys:String>
                    <sys:String>Item4</sys:String>
                    <sys:String>Item5</sys:String>
                </ListBox.Items>
            </ListBox>
        </Grid>
    </Window>
    

    The code-behind:

    namespace WpfApplication12
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {     
            public MainWindow()
            {
                InitializeComponent();
            }
            private void ListBoxItem_DoubleClick(object sender, MouseButtonEventArgs e)
            {
                var listBoxItem = sender as ListBoxItem;
                if (listBoxItem != null)
                {
                    var content = listBoxItem.Content as string;
                    MessageBox.Show(content);
                }
            }
        }
    }
    

    Here is a link to the MSDN page that explains a bit about EventSetters: http://msdn.microsoft.com/en-us/library/system.windows.eventsetter.aspx