4

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
void.pointer
  • 24,859
  • 31
  • 132
  • 243

3 Answers3

6

resizing is cake:

MyCanvas.Width = 350;
MyCanvas.Height  = 450;

this sets the size, you CAN render to coordinates outside of this, but it will be clipped. You can wrap your canvas inside a scroller to allow the user to see what is outside the height/width of the canvas.

as for the rest of your questions, i think you can see this SO question for your answers

Community
  • 1
  • 1
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • Just to clarify for the OP: This code will resize the canvas, but not set it to 350x450 pixels. WPF doesn't use pixels as size units, but 96th parts of an inch; this code sets the width to 3.64in x 4.68in. – Steve Cooper Jan 30 '10 at 18:07
  • I set the size to 478x478 but I did not notice any other controls positioned relative to the bottom of the canvas changing. Perhaps changing the width/height doesn't force the main window to redo the layout? Also keep in mind that I'm setting Width/Height in my Canvas' constructor. I hope that's not too early. – void.pointer Jan 30 '10 at 19:38
  • when you position something in the canvas, its coordinates are relative to the canvas. they dont move automagically. you would have to loop through the child collection and manually adjust. :( – Muad'Dib Jan 31 '10 at 07:47
6

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.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • +1. If you need the inner elements to fit to the constraints of the canvas, then the canvas is probably not the thing to use. A grid with manually set dimensions would be better. – Steve Wortham Jan 30 '10 at 18:11
  • A bit old, use a Viewbox that wrap the Canvas yes. – glihm Sep 22 '20 at 20:10
4

Update 2020: on WPF .NET CORE: If the Canvas is a child of a Viewbox, the content is automatically scaled to the Viewbox height and width.

  <Viewbox Stretch="Fill"
           Width="50"
           Height="50">

     <Canvas>
       <Path.... />
     </Canvas>

  </Viewbox>
glihm
  • 1,138
  • 13
  • 29