#496 – Using a StackPanel to Make a Group of Buttons the Same Size

You’ll often want to stack a group of buttons in a GUI, vertically or horizontally.  You’d typically use a StackPanel to do this.

Let’s say that you want a series of buttons stacked vertically on the right side of a window.

We can use a DockPanel as the main container and add a StackPanel docked on the right and oriented vertically.  But when we do this, the StackPanel expands to fill the available space, as does each Button.

The HorizontalAlignment of the StackPanel defaults to Stretch, as do each of the buttons.  We could set the HorizontalAlignment for each Button to Right, but the buttons now all size to fit their content, which is not quite what we want.

What we really want is for the HorizontalAlignment of each Button to be Stretch, but for the HorizontalAlignment of the StackPanel itself to be Right.  This gives us what we want.


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #496 – Using a StackPanel to Make a Group of Buttons the Same Size

  1. Pingback: Dew Drop – February 16, 2012 (#1,267) | Alvin Ashcraft's Morning Dew

  2. Pingback: #497 – Use a UniformGrid to Make a Group of Buttons the Same Size « 2,000 Things You Should Know About WPF

  3. It works but you don’t use your DockPanel at all: you can remove DockPanel.Dock=”Right” or replace DockPanel by a grid with no change.

    You can use your StackPanel with LastChildFill:

    Yes Absolutely

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: