Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a Canvas in WPF?

I'm writing a WPF app that has a Canvas in it. This canvas will be custom rendered at runtime. It's sort of like a game in that it needs to be measured in pixels. I need to be able to set my Canvas to 478x478 pixels (client rectangle size). I don't want any scaling or other resolution-independent steps to take place on my Canvas.

I'm not sure if this is possible in WPF, since its nature is to be resolution independent. My questions:

  1. How do I resize my Canvas at runtime (function to call?)
  2. When I resize my Canvas, is the renderable area (the client rectangle) going to be that size? If not, how can I resize it to make sure the client rectangle is a specific width/height?
  3. Is it possible to set the width/height of the Canvas in Pixels? How does the resolution-independent aspect of WPF interfere with what I'm trying to do?
  4. When I resize my Canvas, will other controls resize appropriately as they have been designed to do in the WPF designer?

Thanks in advance.

like image 750
void.pointer Avatar asked Dec 22 '22 06:12

void.pointer


1 Answers

Any elements positioned in a canvas will not resize or reposition based upon the size of the canvas. So I don't think there's any advantage to setting the size of the canvas. Maybe you should just set a fixed size of the window instead.

Otherwise, just set the Canvas.Width, Height, and ClipToBounds=True and you have a fixed sized canvas that positions its child elements with X/Y coordinates.

Also you should be sure to set SnapsToDevicePixels=True on the canvas so that child elements will have crisp pixel-aligned bounds.

like image 121
Josh Avatar answered Dec 28 '22 08:12

Josh