HorizontalAlignment. WPF controls can be aligned. With the HorizontalAlignment and VerticalAlignment attributes, we anchor controls to edges or center them.
Getting started. First add several buttons (like 5) to a Grid in a Window. I left the default Width of each Button, but changed the alignment properties.
Example. This example shows both HorizontalAlignment and VerticalAlignment. The buttons can be seen aligned in the screenshot on this page.
Width. What happens when you specify both HorizontalAlignment of Stretch, and a Width value? This ends up being the same as Center.
And The Width value takes precedence over Stretch. To have a control expand to fill the space, remove the Width or Height properties.
A discussion. The example only uses Buttons, but many controls support the HorizontalAlignment and VerticalAlignment properties. In many ways, all controls are unified in WPF.
A summary. HorizontalAlignment and VerticalAlignment align controls to one edge of the container. With them, we also can Stretch or Center the controls.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.