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>
This can be easily achieved by creating an ItemContainerStyle and adding an EventSetter.
Modify your XAML like so:
The code-behind:
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