简体   繁体   中英

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. each of the hexagons is a button and a child of the Grid, and they have a style that displays them as hexagons.

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 .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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