单击WPF组合框项或按Enter键选择时如何捕获事件

单击WPF组合框项或按Enter键选择时如何捕获事件

本文介绍了单击WPF组合框项或按Enter键选择时如何捕获事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几个小时,但是没有用.

I had tried this for few hours, but it's not working.

我有一个组合框,里面有一些项目,像搜索框一样动态生成.

I have a combobox, with a few items in there, generated dynamically like a search box.

现在,当用户单击下拉菜单项或单击下拉菜单项时,我想捕获一个事件.

Now I want to capture an event, when user click on the dropdown menu item, or click on the dropdown menu item.

如何实现这一目标?我试图在Combobox上设置鼠标/键盘事件处理程序,但它仅在组合框的文本框中起作用,而在下拉列表中不起作用.

How to achieve this? I tried to set mouse/keyboard event handler on Combobox, but it only works on the combobox's textbox, not in the dropdown list.

谢谢.

我忘了提到我在组合框上自定义了DataTemplate.我尝试了另一种在ComboBox.ItemContainerStyle中设置事件的方法.

I forgot to mention that I has custom DataTemplate on my Combobox. I tried another approach which set the event in ComboBox.ItemContainerStyle.

我尝试了PreviewKeyDown,但未捕获.有什么主意吗?

I tried PreviewKeyDown, but it is not captured. Any idea?

推荐答案

而不是使用 MouseLeftButtonDown 事件,使用 PreviewMouseLeftButtonDown 事件

instead of using the MouseLeftButtonDown event,use the PreviewMouseLeftButtonDown event

WPF支持事件冒泡"概念,即触发事件时,它将在实现该事件的树上冒出一个较高的元素.但是ComboBox本身已经实现了click事件.所以必须告诉它冒泡".

WPF supports the "event bubbling" concept, that when an event is fired, it bubbles up the an higher element on the tree that implements that event.but the ComboBox itself already implements the click event. so you have to tell it to bubble "down".

这篇关于单击WPF组合框项或按Enter键选择时如何捕获事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 09:19