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



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

i have simple wpf application listbox of items. simplicity of example, made list of strings, in reality, list of complex type. when item in listbox double clicked, want respond it. apparently, there no direct double click event on listbox item itself. there simple way of responding item in listbox beingness double-clicked (not listbox itself)?

here 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 achieved creating itemcontainerstyle , adding eventsetter.

modify xaml 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 mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); } private void listboxitem_doubleclick(object sender, mousebuttoneventargs e) { var listboxitem = sender listboxitem; if (listboxitem != null) { var content = listboxitem.content string; messagebox.show(content); } } } }

here link msdn page explains bit eventsetters: http://msdn.microsoft.com/en-us/library/system.windows.eventsetter.aspx

wpf listbox eventsetter

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -