WPF: How can i scale to fit the content?

2019-08-08 20:26发布

问题:

It gets basicaly to this: I have a canvas container (x:name="Container" - to reffer later to), with some other controls(e.g. I have a Button 30x60 in size) and I want to scale the Container to fit the button, without having to resize anything. (just using scaleTransform) If I set a slider binded to the Container Scale, I can slide it to fit the button, but the value is 1.1343 and I want to be able somehow to compute this value programatically, because all the controls inside Container are of different sizes, and resizable in adition. The question is: is there a way to scale the Container so that a Control from the container is displayed on the entire Container surface? Also the position will have to be adjusted, so that the scrolling vierew of the Container should be positioned over the control, but that is done already.

Thank you in advance, Daniel

回答1:

Use Grid as the container. Any control placed inside of the Grid will directly fill the whole Grid, and its size (the width and height) will match the size of the Grid.



回答2:

It seems when you add a user control using a grid as a container, the usercontrol, which comprises a set of labels and an array of buttons will not align to the top but arranged somewhere in the middle.