开发者

MouseLeftButtonUpEvent & Bubbling in Canvas

I think I'm going crazy...

I have Canvas with event handlers for MouseMove & MouseLeftButtonUp. However MouseLeftButtonUp is not being fired when it happens with cursor over TextBlock that is inside canvas. (it fires just fine when I release mouse button in empty space of the canvas)

开发者_运维问答

I tried attaching handler via AddHandler and using regular += syntax, nothing seems to work. I tried using Canvas.CaptureMouse() but it doesnt seem to work either (CaptureMouse returns true btw).

MouseLeftButtonUp just doesnt want to propagate to it's parent when it happens over TextBlock (or any other element with IsHitTestVisible = true) inside Canvas.

Please help.


First I'd like to say that you are not going crazy. I've seen this before in Silverlight applications. Silverlight has some interesting event strategies. Silverlight events follow a bubble up approach for routed events but not with all events (msdn has some information on this) The events you are listening to are in that list but they are being handled by the TextBlock. Most UIElements have IsHitTestVisible=true so that mouse events and others are captured by the control and not bubbled up to its parent. Setting IsHitTestVisible=false should solve the problem. Other than that I can tell you what I have tried to overcome this issue when needing IsHitTestVisible=true.

  1. Set the event hanlder from the parent on the TextBlock. Downside is you need to do this for every control in your canvas.
  2. Try to fire the event through an extension class. I could not get this one to work cause i couldn't fire events using reflection.


You mentioned you tried AddHandler - did you try the AddHandler overload that accepts two parameters, the second one being "true" to indicate you want to get handled events as well?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜