开发者

MouseEnter and MouseLeave processing in WPF (using VS2008 and .net 3.5) not working as expected

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 fo开发者_JAVA百科llows (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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜