Guidelines for the WPF layout
These are the five most popular layout panels of WPF:
Grid Panel
Stack Panel
Dock Panel
Wrap Panel
Canvas Panel
How to use them better, you should follow these guidelines:
- Avoid fixed positions – use the
Alignmentproperties in combination withMarginto position elements in a panel - Avoid fixed sizes – set the
WidthandHeightof elements toAutowhenever possible. - Don’t abuse the canvas panel to layout elements. Use it only for vector graphics.
- Use a StackPanel to layout buttons of a dialog
- Use a GridPanel to layout a static data entry form. Create a Auto sized column for the labels and a Star sized column for the TextBoxes.
- Use an ItemControl with a grid panel in a DataTemplate to layout dynamic key value lists. Use the SharedSize feature to synchronize the label widths.
