Alan Macdonald
Alan Macdonald

Reputation: 1900

DrawingVisual Mouse Up Event

I am working on a WPF MVVM application. It has a custom canvas class inheriting from Canvas. There is a window with one of these custom canvas placed (using XAML) and it's background colour set to Transaparent. Graphics are drawn to the Canvas using DrawingVisuals to draw primitive shapes and add them as Visual children to the Visual tree. This works and renders my shapes to the screen.

I have added a mouse up event to the Canvas in the Window code behind. This fires when I click anywhere on the canvas except on the line of a drawn visual or on the coloured area of a filled shape. To be clear:

From http://msdn.microsoft.com/en-us/library/ms742254.aspx#providing_hit_testing_support it appears like this should work though I am using the Canvas MouseUp event as it does not have a MouseLeftButtonUp event. In the MS example they don't use a Canvas but talk about a "host container" but I'm not sure if that's relevant.

How can I get the events to fire when clicking on a DrawingVisual? Why is the event surpressed when clicking inside a drawn area but not outside it?

Thanks

Upvotes: 2

Views: 1480

Answers (4)

Alan Macdonald
Alan Macdonald

Reputation: 1900

I have solved the problem. The canvas has a RootNode dependency property which the ViewModel sets. The problem is when you add any children to a canvas then you need to call AddVisualChild(child) and AddLogicalChild(child). I had to add a property changed callback to the dependency property so it could call the two methods to add them as visual and logical children otherwise it doesn't understand the parent child relationships which screws up user input.

Thanks for you time everyone. Sorry if I never provided enough details. It's a big app so was trying to include only relevant info.

Upvotes: 1

Have you tried subscribing to the PreviewMouseUp event as well as MouseLeftButtonUp on your parent Canvas? The above should work, but something in the visual tree is likely marking the event as Handled so it isn't propagating.

Handling Preview ensures you get the event first (as previews tunnel down, then events bubble up, but if any preview event is handled, no further events are bubbled).

There's a detailed overview here: http://msdn.microsoft.com/en-us/library/ms747183.aspx

In addition, if that fails, you can always try a class handler on the event (basically an override of the above behaviour): http://msdn.microsoft.com/en-us/library/ms747183.aspx#AddingInstanceHandlersthatAreRaisedEvenWhenEventsareMarkedHandled

Upvotes: 0

Ibraheem Osama
Ibraheem Osama

Reputation: 334

You should read this article about routed event http://msdn.microsoft.com/en-us/library/ms742806.aspx And how routed event will cross the rectangle to get to the main canvas :)

Upvotes: 0

GazTheDestroyer
GazTheDestroyer

Reputation: 21261

I suspect this may be because Canvas is doing the hit testing for you, and thus mouse clicks are getting routed to the child visuals.

Can you try deriving from FrameworkElement instead. Is there some behaviour of Canvas that you need?

Upvotes: 0

Related Questions