In my WPF app I'm handling a ListBox SelectionChanged event and it runs fine.
Now I need to handle a click event (even for the already selected item); I've tried MouseDown but it does not work. How can I handle a ListBox click on an item?
Just handle PreviewMouseDown event:
private void listBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var item = ItemsControl.ContainerFromElement(listBox, e.OriginalSource as DependencyObject) as ListBoxItem;
if (item != null)
{
// ListBox item clicked - do some cool things here
}
}
Perhaps try the PreviewMouseDown event. The MouseDown event gets swallowed and converted to the SelectionChanged event.
Only downside is that the PreviewMouseDown will occur before the SelectionChanged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With