Canvas, Shapes, and Events. What's happening here?

I've noticed some interesting behaviour with events on a Canvas, and I was just wondering what is going on.

I have a custom GridControl with a Canvas on it, that has a handler for the Canvas MouseDown event.

uiElement.MouseDown += new System.Windows.Input.MouseButtonEventHandler(uiElement_MouseDown);

I've added some PolyLines to the canvas, and I've noticed that if I click on the lines, then my event handler doesn't get called. So ok I add an event handler to the PreviewMouseDown event to see if I can get the event before it gets to the Shape, but that doesn't work.

OK, I put a transparent Canvas over the top of it all, and I was going to handle the MouseDown event from the transparent Canvas, but I notice that now I am getting all the MouseDown events I need from the Canvas underneath. What's going on here Is there some documentation somewhere that explains what is going on



Answer this question

Canvas, Shapes, and Events. What's happening here?

  • DaveIHS01

    Yes that is what I thought should happen. But the shapes seem to be absorbing the event, and I haven't added any event handlers to the shapes.
  • fantasimus

    my bad, just spotted I was hooking into the Grid.MouseDown which explains why I still got the events when I put the transparent canvas over the top. Even so, there still seems to be the problem that Shapes on a Canvas are absorbing events.

    How to I get the events from the Shapes to be routed to my canvas I was under the impression that PreviewMouseDown could be used to get the events before they got to the shapes.


  • Coder0xff

    I don't have the problem if I use a Path instead, so I've switched to using that
  • James Hunter

    If the shapes are children of the Canvas and the Canvas handles MouseLeftButtonDown and the shapes do not then clicking on the shapes will activate the Canvas' handler.
  • Canvas, Shapes, and Events. What's happening here?