WPF 中的 MouseEnter 和 MouseLeave 处理(使用 VS2008 和 .net 3.5)未按预期工作
没有图像、没有代码,要解释这个问题并不容易。对于那些感兴趣的人,我制作了一个示例 C#/WPF 项目,可以从 http:// /rapidshare.com/files/461745095/02.WPFControlEvents.rar
这是一个非常小的项目(45KB)。问题如下(我对扭曲的描述表示歉意):
一个“花哨”(而且相当丑陋)的按钮包含一个堆栈面板,该面板又包含1.)一个标签,2.)一个画布,3.)两个(2 )同心椭圆,全部从左到右排列。
stackpanel 有一个 MouseEnter 和 MouseLeave 事件处理程序,它在标题栏中显示一条消息,指示鼠标是在 stackpanel 上方还是在其外部。
问题是:当鼠标位于标签上方(又包含在堆栈面板中)时,鼠标(正确地)报告为位于堆栈面板上方。当鼠标移到画布上(也包含在堆栈面板中)时,鼠标(错误地?)报告为不在堆栈面板上,但是当鼠标进一步移动到右侧(在画布上的两个椭圆上方),据报告鼠标再次位于堆栈面板上。
为什么当鼠标位于画布上时,它被报告为不在堆栈面板上,但当鼠标位于省略号(绘制在画布上)上时,它被报告为位于堆栈面板上?
约翰,谢谢你揭示了这个问题
。
It is not easy to explain the problem without an image and without code. For those interested, I have made a sample C#/WPF project that can be downloaded from http://rapidshare.com/files/461745095/02.WPFControlEvents.rar
It is a very small project (45KB). The problem is as follows (with my apologies for the contorted description):
A "fancy" (and rather ugly) button contains a stackpanel which in turn contains 1.) a label, 2.) a canvas, 3.) two (2) concentric ellipses, all laid out from left to right.
There is a MouseEnter and a MouseLeave event handler for the stackpanel which displays a message in the title bar indicating whether the mouse is over the stackpanel or outside of it.
The problem is: when the mouse is over the label (in turn contained in the stackpanel), the mouse is (correctly) reported as being over the stackpanel. When the mouse is moved over the canvas (which is also contained in the stackpanel), the mouse is (incorrectly?) reported as not being over the stackpanel, yet when the mouse is moved a little further to the right (over the two ellipses, which are on the canvas), the mouse is reported to be over the stackpanel again.
Why is it that when the mouse is over the canvas it is reported as not being over the stackpanel but when the mouse is over the ellipses (which are painted on the canvas) it is reported as being on the stackpanel ?.
thank you for shedding light into the problem,
John.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要将画布的背景设置为透明。这将允许它“可测试”并报告鼠标悬停事件。
更多信息可以在此处找到,但 Canvas 默认情况下具有空背景。
Chances are you need to set the Background of the Canvas to Transparent. This will allow it to be "hit testable" and report mouse over events.
More info can be found here, but Canvas has a null background by default.