Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Canvas: centered origin, scaled axes (and y inverted), responding to mouse events
    text
    copied!<p>In a simple code-only WPF application I'm writing, I would like to have a custom <code>Canvas</code>.</p> <p>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).</p> <p>In my canvas, I'd like it to have the following properties:</p> <ol> <li>the point (0, 0) is on the center of the <code>Canvas</code>;</li> <li>the x-axis points to the right;</li> <li>the y-axis points to the top;</li> <li>the point (1, 0) is about 1 inch to the right of the origin; and</li> <li>in every event, the position of the mouse is given in the coordinate system defined above.</li> </ol> <p>In <a href="https://stackoverflow.com/questions/1871358/wpf-how-to-make-the-0-0-in-center-inside-a-canvas/1965257#1965257">this answer</a>, 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 well (change <code>1</code> and <code>-1</code> in the <code>RenderTransform</code> to other constants).</p> <p>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, <strong>there's no canvas</strong> to capture events like a click, so this solution won't handle my fifth property.</p> <p>What's the easiest way to achieve this? Inherit from <code>Panel</code> and do everything by hand? Inherit from <code>Canvas</code>, intercept every event and modify the coordinates?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload