Nasıl yapılır: ListView'daki Her Öğe için MouseDoubleClick Olayını İşleme
bir öğesinde bir olay işlemek için her bir olay ListView işleyicisi eklemeniz ListViewItem gerekir. bir veri kaynağına bağlı olduğunda, açıkça oluşturmaz, ancak bir stiline ekleyerek her öğe için olayı ListViewListViewItemEventSetterListViewItem işebilirsiniz.
Örnek
Aşağıdaki örnek bir veri bağımlı oluşturur ve ListView her bir için bir olay Style işleyicisi eklemek için ListViewItem oluşturur.
<!--XmlDataProvider is defined in a ResourceDictionary,
such as Window.Resources-->
<XmlDataProvider x:Key="InventoryData" XPath="Books">
<x:XData>
<Books xmlns="">
<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>
Aşağıdaki örnek olayı MouseDoubleClick ele almaktadır.
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.");
}
}
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
Not
Bir veri kaynağına bağlamak en yaygın olsa da, açıkça oluşturmanızdan bağımsız olarak veri bağımlı olmayan bir içinde her biri için bir olay işleyicisi eklemek için ListViewListViewItem bir stil ListViewListViewItem kullanabilirsiniz. Açıkça ve örtülü olarak oluşturulan denetimler hakkında daha fazla bilgi için ListViewItem bkz. ItemsControl .