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:
- If I click outside the drawn shape i.e. on the background of the
canvas it fires. - If there is an unfilled rectangle and I click on the inside blank
area then the mouse up fires. - If I click on the rectangle’s border line it does not.
- If I click inside a filled rectangle it does not.
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
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.