views:

45

answers:

2

Hello smart people.

I'm just getting into C# and some unfamiliar piece of code so apologies if my question is not well asked.

The problem in short - I have a six by eight grid of System.Windows.Controls.Canvas objects. The top left canvas does not receive mouse events while all the others do (I tried OnMouseMove and OnMouseClick events).

I went through the MSDN documentation Routed Events Overview. Suspecting that some child element may be handling the event, I used System.Windows.Media.VisualTreeHelper#GetChildrenCount() to see if the object has any children. It doesn't have any. I also tried catching PreviewOnMouseMove with no luck. I'm baffled as to who along the visual tree is eating the event. Can there be some (maybe transparent) control in front of my canvas that takes precedence in handling the event? As a Canvas object, can I know who's in front of me?

Thanks!

A: 

I hope this helps you, Just set Transparent value to the Background property for the Canvas

Background="Transparent"

Good luck!

Homam
I tried this with no joy.
tilish
Homam
I tried for the Canvas only - but just for the record do we have any explanation as to why setting a backgrounds to transparent might help? EDIT: (It would be useful to know)
tilish
Ok, Check this link http://forums.silverlight.net/forums/p/60500/151266.aspx
Homam
A: 

I finally got around this problem. Each of the canvases have adorners. According to this MSDN article on adorners, adorners receive mouse events before the underlying UI element does. So I set the IsHitTestVisible property of all my adorners to false. My situation is a bit different because the code I'm dealing with removes and adds adorners in a complex way which somehow ends up giving an odd behavior on the top left cell. I'd rather not write all my guesses here but letting my adorners pass the event through fixes my problem.

If anyone runs into the problem of not knowing where mouse events are disappearing, I suggest you read this nice introduction to Logical and Visual trees and Routed Events. It will give you a good idea of how events travel.

My understanding so far is if your mouse is over a UI element but the element is not receiving events then two things might be happening,

  1. Your element has a child element and the child is handling the event. If you are clueless, you will find System.Windows.Media.VisualTreeHelper helpful. Put a break point somewhere sensible and use the Immediate Window to query for the children of the control in question.

  2. There is another element in front of your element that has a higher z order. This happens in case of adorners for example. There may be other situations that I am not aware of. I hope people will give more comments on this.

tilish