How to get controls in WPF to fill available space?

前端 未结 4 2094
面向向阳花
面向向阳花 2020-11-27 09:47

Some WPF controls (like the Button) seem to happily consume all the available space in its\' container if you don\'t specify the height it is to have.

A

4条回答
  •  轮回少年
    2020-11-27 10:16

    Use the HorizontalAlignment and VerticalAlignment layout properties. They control how an element uses the space it has inside its parent when more room is available than it required by the element.

    The width of a StackPanel, for example, will be as wide as the widest element it contains. So, all narrower elements have a bit of excess space. The alignment properties control what the child element does with the extra space.

    The default value for both properties is Stretch, so the child element is stretched to fill all available space. Additional options include Left, Center and Right for HorizontalAlignment and Top, Center and Bottom for VerticalAlignment.

提交回复
热议问题