"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"
If you resize the window, you??™ll see that the label remains in the middle and the first two buttons stay stuck to either side. CHAPTER 4 n LAYOUT 84 Figure 4-4. A StackPanel with aligned buttons nNote The StackPanel also has its own HorizontalAlignment and VerticalAlignment properties. By default, both of these are set to Stretch, and so the StackPanel fills its container completely. In this example, that means the StackPanel fills the window. If you use different settings, the StackPanel will be made just large enough to fit the widest control. Margin There??™s an obvious problem with the StackPanel example in its current form. A well-designed window doesn??™t just contain elements??”it also includes a bit of extra space in between the elements. To introduce this extra space and make the StackPanel example less cramped, you can set control margins. When setting margins, you can set a single width for all sides, like this:
Alternatively, you can set different margins for each side of a control in the order left, top, right, bottom:
In code, margins are set using the Thickness structure: cmd.Margin = New Thickness(5) Getting the right control margins is a bit of an art because you need to consider how the margin settings of adjacent controls influence one another.