开发者

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:

  1. the point (0, 0) is on the center of the Canvas;
  2. the x-axis points to the right;
  3. the y-axis points to the top;
  4. the point (1, 0) is about 1 inch to the right of the origin; and
  5. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜