WPF Canvas: centered origin, scaled axes (and y inverted), responding to mouse events
In a simple code-only WPF application I'm writing, I would like to have a custom Canvas
.
I've found questions similar to this one here in StackOverflow, but couldn't find exactly this one, nor a simple way to adapt another answer to my specific problem (please note that I have not much experience in WPF).
In my canvas, I'd like it to have the following properties:
- the point (0, 0) is on the center of the
Canvas
; - the x-axis points to the right;
- the y-axis points to the top;
- the point (1, 0) is about 1 inch to the right of the origin; and
- in every event, the position of the mouse is given in the coordinate system defined above.
In this answer, Ray Burns propose a very simple solution to my first 3 points. It is trivial to modify that code so that it deals with my point number 4 as we开发者_JAVA百科ll (change 1
and -1
in the RenderTransform
to other constants).
However, that very simple and excellent solution for many problems is based on setting width and height to 0, and centering the canvas on its container. Therefore, there's no canvas to capture events like a click, so this solution won't handle my fifth property.
What's the easiest way to achieve this? Inherit from Panel
and do everything by hand? Inherit from Canvas
, intercept every event and modify the coordinates?
Put Canvas
inside a Border
. Border
has mouse events. Another case is to place Border
on the top of the Canvas
.
精彩评论