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 pix
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>
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
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.