Answered by:
Canvas Sizing

Question
-
I'm using a Canvas to explicitly position child controls.
Is there a simple (if not built-in) method to size the canvas and all it's children such that it fits the size of the screen ?
Thanks
Friday, June 17, 2016 4:04 PM
Answers
-
One of the odd aspects of a canvas is that it does not clip it's children.
Even if you could persuade a canvas to clip, it would just chop bits off controls which didn't fit.
That means there is no simple method to size the children of a canvas such that they fit the size of the screen.
You would have to iterate the children, check their Canvas.Left and Canvas.Top attached properties, their height and width and compare to the canvas height and width.
Then do whatever logic you can come up with to make them fit.
This is one of the many reasons not to use a canvas to explicitly position child controls unless you actually want to allow controls to position off screen.
Maybe you should be using a panel like a Grid instead.
- Proposed as answer by DotNet Wang Saturday, June 25, 2016 8:19 AM
- Marked as answer by DotNet Wang Monday, June 27, 2016 9:51 AM
Friday, June 17, 2016 5:56 PM
All replies
-
One of the odd aspects of a canvas is that it does not clip it's children.
Even if you could persuade a canvas to clip, it would just chop bits off controls which didn't fit.
That means there is no simple method to size the children of a canvas such that they fit the size of the screen.
You would have to iterate the children, check their Canvas.Left and Canvas.Top attached properties, their height and width and compare to the canvas height and width.
Then do whatever logic you can come up with to make them fit.
This is one of the many reasons not to use a canvas to explicitly position child controls unless you actually want to allow controls to position off screen.
Maybe you should be using a panel like a Grid instead.
- Proposed as answer by DotNet Wang Saturday, June 25, 2016 8:19 AM
- Marked as answer by DotNet Wang Monday, June 27, 2016 9:51 AM
Friday, June 17, 2016 5:56 PM -
>>Is there a simple (if not built-in) method to size the canvas and all it's children such that it fits the size of the screen ?
Short answer: No.
One of the characteristics of the Canvas panel is that is has no inherent layout characteristics and it doesn't resizes its children, the child elements are just positioned at their designated coordinates.
If you want a panel that resizes itself to fit the available screen space, you should choose another one. You could for example try to add all child elements to a Grid and use the Margin property to position them, e.g.:
<Grid Width="400" Height="400"> <Button Content="A" Margin="100 100 10 10" MaxWidth="50" MaxHeight="50" /> <Button Content="B" Margin="200 10 10 200" MaxWidth="50" MaxHeight="50" /> </Grid>
Or you could try to create your own custom Canvas panel. Please refer to the following thread for more information: http://stackoverflow.com/questions/855334/wpf-how-to-make-canvas-auto-resize
Hope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by DotNet Wang Saturday, June 25, 2016 8:19 AM
- Marked as answer by DotNet Wang Monday, June 27, 2016 9:51 AM
- Unmarked as answer by theresonlyonefinmaf Wednesday, July 6, 2016 11:06 AM
Saturday, June 18, 2016 10:59 AM -
How about using a ScaleTransform ?
Any ideas how to convert the size of the page to the correct scale factor ?
Wednesday, July 6, 2016 11:08 AM