How to: Handle the MouseDoubleClick Event for Each Item in a ListView
To handle an event for an item in a ListView, you need to add an event handler to each ListViewItem. When a ListView is bound to a data source, you don't explicitly create a ListViewItem, but you can handle the event for each item by adding an EventSetter to a style of a ListViewItem.
Example
The following example creates a data-bound ListView and creates a Style to add an event handler to each ListViewItem.
<!--XmlDataProvider is defined in a ResourceDictionary,
such as Window.Resources-->
<XmlDataProvider x:Key="InventoryData" XPath="Books">
<x:XData>
<Books >
<Book ISBN="0-7356-0562-9" Stock="in" Number="9">
<Title>XML in Action</Title>
<Summary>XML Web Technology</Summary>
</Book>
<Book ISBN="0-7356-1370-2" Stock="in" Number="8">
<Title>Programming Microsoft Windows With C#</Title>
<Summary>C# Programming using the .NET Framework</Summary>
</Book>
<Book ISBN="0-7356-1288-9" Stock="out" Number="7">
<Title>Inside C#</Title>
<Summary>C# Language Programming</Summary>
</Book>
<Book ISBN="0-7356-1377-X" Stock="in" Number="5">
<Title>Introducing Microsoft .NET</Title>
<Summary>Overview of .NET Technology</Summary>
</Book>
<Book ISBN="0-7356-1448-2" Stock="out" Number="4">
<Title>Microsoft C# Language Specifications</Title>
<Summary>The C# language definition</Summary>
</Book>
</Books>
</x:XData>
</XmlDataProvider>
...
<!--The Style is defined in a ResourceDictionary,
such as Window.Resources-->
<Style TargetType="ListViewItem">
<EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick"/>
</Style>
...
<ListView ItemsSource="{Binding Source={StaticResource InventoryData}, XPath=Book}">
<ListView.View>
<GridView>
<GridViewColumn Width="300" Header="Title"
DisplayMemberBinding="{Binding XPath=Title}"/>
<GridViewColumn Width="150" Header="ISBN"
DisplayMemberBinding="{Binding XPath=@ISBN}"/>
</GridView>
</ListView.View>
</ListView>
The following example handles the MouseDoubleClick event.
Private Sub ListViewItem_MouseDoubleClick(ByVal sender As Object, _
ByVal e As MouseButtonEventArgs)
Dim lvi As ListViewItem = CType(sender, ListViewItem)
Dim book As XmlElement = CType(lvi.Content, XmlElement)
If book.GetAttribute("Stock") = "out" Then
MessageBox.Show("Time to order more copies of " + book("Title").InnerText)
Else
MessageBox.Show(book("Title").InnerText + " is available.")
End If
End Sub
void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
XmlElement book = ((ListViewItem) sender).Content as XmlElement;
if (book == null)
{
return;
}
if (book.GetAttribute("Stock") == "out")
{
MessageBox.Show("Time to order more copies of " + book["Title"].InnerText);
}
else
{
MessageBox.Show(book["Title"].InnerText + " is available.");
}
}
Note
Although it is most common to bind a ListView to a data source, you can use a style to add an event handler to each ListViewItem in a non-data-bound ListView regardless of whether you explicitly create a ListViewItem. For more information about explicitly and implicitly created ListViewItem controls, see "Item Container Classes" in Controls Content Model Overview.
See Also
Tasks
How to: Bind to XML Data Using an XMLDataProvider and XPath Queries