简体   繁体   English

wpf:根据控件内容调整控件大小

[英]wpf: resizing a control according to its content

I have a control that inherits from Grid, it is a grid of hexagons that are generated dynamically according to the properties.我有一个从 Grid 继承的控件,它是根据属性动态生成的六边形网格。 each of the hexagons is a button and a child of the Grid, and they have a style that displays them as hexagons.每个六边形都是 Grid 的一个按钮和一个子项,并且它们具有将它们显示为六边形的样式。

what I want is for the grid to change its size according to the total size of the hexagons.我想要的是网格根据六边形的总大小来改变它的大小。 (I can calculate the exact size needed, but I don't know how to set it). (我可以计算出所需的确切尺寸,但我不知道如何设置)。

Basically you've got several options.基本上你有几个选择。 A simple one is calculating the size yourself and assigning to the Grid's Width and Height .一个简单的方法是自己计算大小并分配给 Grid 的WidthHeight

A more elaborate solution would be to ask yourself a question: which layout is needed for my items?一个更复杂的解决方案是问自己一个问题:我的项目需要哪种布局 There are some standard containers which do the layout themselves and can grow/shrink with the content.有一些标准容器可以自己进行布局,并且可以随内容增长/缩小。 For example, if your objects are just aligned in a line, you can go for StackPanel .例如,如果您的对象只是在一条线上对齐,您可以为StackPanel使用 go 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM