WPF: Canvas mouse events not firing on empty space
Asked Answered
D

2

45

I have set mouse events on a Canvas as follows:

<Canvas MouseUp="CanvasUp" MouseDown="CanvasDown" MouseMove="CanvasMove">
...
</Canvas>

But these are active only on the child elements like Image and Rectangle, not on the empty space. How can I solve this?

Dumpy answered 24/3, 2010 at 11:20 Comment(0)
M
102

A control with no background color set (explicitly or through styles etc) will default to having a background color of null - making it not hit-testable.

If you set the background to "Transparent" (or anything other than null ({x:Null})) then it will be able to pick up the mouse events

Montero answered 24/3, 2010 at 11:38 Comment(2)
Thanks for the answer, but what a STUPID design decision the WPF team did with that.Douglas
This advice is valuable a golden coin ! I'd rather default Canvas background to Transparent, rather than {x:null} as normally you'd expect event (with appropriate arguments) to fire when you click on canvas empty space. The default x:null behavior should be optionally settable, rather.Ariel
C
6

Set the background color. It defaults to null.

Use Background="White" for instance.

Cootie answered 24/3, 2010 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.