tag:blogger.com,1999:blog-6579772240267288367.post4541532919148957923..comments2023-06-05T08:45:12.716-04:00Comments on kwblog: WPF ListBoxItem Double ClickKevin Berridgehttp://www.blogger.com/profile/13759114853595462455noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-6579772240267288367.post-7188313263845868392012-05-10T07:16:51.855-04:002012-05-10T07:16:51.855-04:00Other possibility, i built based on an example of ...Other possibility, i built based on an example of handling a doubleclick in an datagrid<br /> <br /> private MyClass ListBox_MouseDoubleClick(object sender,<br /> System.Windows.Input.MouseButtonEventArgs e)<br /> {<br /> IInputElement element = e.MouseDevice.DirectlyOver;<br /> if (element != null && element is FrameworkElement)<br /> {<br /> if (((System.Windows.FrameworkElement)element).TemplatedParent is ContentPresenter)<br /> return (((System.Windows.FrameworkElement)element).TemplatedParent as ContentPresenter).Content as MyClass;<br /> else if (((System.Windows.FrameworkElement)element).TemplatedParent is ListBoxItem)<br /> return (((System.Windows.FrameworkElement)element).TemplatedParent as ListBoxItem).Content as MyClass;<br /> }<br /> return null;<br /> } }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6579772240267288367.post-51031483026962002692011-07-22T08:57:53.975-04:002011-07-22T08:57:53.975-04:00Another very simple solution :
Private Sub ListBo...Another very simple solution :<br /><br />Private Sub ListBox_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles ListBox.MouseDoubleClick<br /> ' If mouse is over ScrollBar then exit<br /> If e.MouseDevice.DirectlyOver.GetType Is GetType(System.Windows.Controls.Primitives.Thumb) _<br /> Or e.MouseDevice.DirectlyOver.GetType Is GetType(System.Windows.Controls.Primitives.RepeatButton) Then Exit Sub<br /><br /> ' do the job here<br />End SubAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-6579772240267288367.post-42586690268009767092010-11-26T15:31:31.749-05:002010-11-26T15:31:31.749-05:00Just wanted to say thanks for the post! Saved me s...Just wanted to say thanks for the post! Saved me some hassle.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6579772240267288367.post-423995799500986022009-07-30T17:19:18.789-04:002009-07-30T17:19:18.789-04:00Thanks for the post Kevin. I have translated your ...Thanks for the post Kevin. I have translated your XAML code into C# and it works nicely with ListBoxes as well as ListViews. Here it is:<br /><br />private void AddDoubleClickEventStyle(ListBox listBox, MouseButtonEventHandler mouseButtonEventHandler)<br /> {<br /> if (listBox.ItemContainerStyle == null)<br /> listBox.ItemContainerStyle = new Style(typeof(ListBoxItem));<br /> listBox.ItemContainerStyle.Setters.Add(new EventSetter()<br /> {<br /> Event = MouseDoubleClickEvent,<br /> Handler = mouseButtonEventHandler<br /> });<br /> }<br /><br /><br />Usage:<br /> AddDoubleClickEventStyle(listView1, new MouseButtonEventHandler(listView1_MouseDoubleClick));<br /><br />CheersZiadnoreply@blogger.comtag:blogger.com,1999:blog-6579772240267288367.post-341210797825986632009-01-14T10:13:00.000-05:002009-01-14T10:13:00.000-05:00The approach Steve listed is better. There potent...The approach Steve listed is better. There potentially is an issue where InputHitTest may not return a UIElement. For example if you had a Span element buried in the list box item. That will throw an exception.<BR/><BR/>I've switched to the other approach and this issue goes away.Mark Boltuchttps://www.blogger.com/profile/08238687339829113099noreply@blogger.comtag:blogger.com,1999:blog-6579772240267288367.post-18277998977407524832008-10-16T11:57:00.000-04:002008-10-16T11:57:00.000-04:00Hey Kevin - I stumbled across your post while tryi...Hey Kevin - <BR/><BR/>I stumbled across your post while trying to find the same answer, and luckily found it somewhere else.<BR/><BR/>Here's an excerpt:<BR/> <ListBox.ItemContainerStyle><BR/> <Style TargetType="{x:Type ListBoxItem}"><BR/> <EventSetter Event="MouseDoubleClick" Handler="listBoxItem_DoubleClick" /><BR/> </Style><BR/></ListBox.ItemContainerStyle><BR/><BR/>Here's the link to the MSDN blog that answered the question:<BR/><BR/>http://blogs.msdn.com/wpfsdk/archive/2008/04/30/itemcontrols-don-t-properly-select-items-or-raise-events-when-bound-to-a-data-source.aspxUnknownhttps://www.blogger.com/profile/13397390440267625704noreply@blogger.com