在我的WPF应用程序中,我正在处理一个ListBox的SelectionChanged事件,它运行良好。
现在我需要处理一个点击事件(即使是对已选择的项目);我尝试使用MouseDown但它不起作用。如何处理ListBox上的项目点击?
在我的WPF应用程序中,我正在处理一个ListBox的SelectionChanged事件,它运行良好。
现在我需要处理一个点击事件(即使是对已选择的项目);我尝试使用MouseDown但它不起作用。如何处理ListBox上的项目点击?
只需处理PreviewMouseDown事件:
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
}
}
PreviewMouseDown
事件。而MouseDown
事件会被吞噬并转换为SelectionChanged
事件。PreviewMouseDown
事件会先于SelectionChanged
发生。 EventManager.RegisterClassHandler(typeof(ListBoxItem), ListBoxItem.MouseLeftButtonDownEvent, new RoutedEventHandler(EventBasedMouseLeftButtonHandler));
private static void EventBasedMouseLeftButtonHandler(object sender, RoutedEventArgs e)
{
}
请告诉我这是否有所帮助...