WPF Layout Tutorials
Welcome to the comprehensive guide on WPF layout in .NET. This section covers various panels and techniques to arrange your UI elements effectively.
StackPanel
The StackPanel arranges child elements in a single line, either horizontally or vertically. It's simple and often used for lists of items.
Orientation: Can be set to Horizontal or Vertical (default is Vertical).
<StackPanel Orientation="Horizontal">
<Button Content="Button 1"/>
<Button Content="Button 2"/>
<Button Content="Button 3"/>
</StackPanel>
Grid
The Grid is a powerful and flexible panel that divides its space into rows and columns. You can specify the size of rows and columns using absolute values, percentages, or the Auto and * (star) sizing options.
Key Features:
RowDefinitionsandColumnDefinitionsdefine the layout structure.- Elements can be placed in specific cells using
Grid.RowandGrid.Columnattached properties. RowSpanandColumnSpanallow elements to occupy multiple cells.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Header" Grid.Row="0" Grid.ColumnSpan="2"/>
<Border BorderBrush="Gray" BorderThickness="1" Grid.Row="1" Grid.Column="0"/>
<TextBlock Text="Content Area 1" Grid.Row="1" Grid.Column="1" Margin="5"/>
<Button Content="Footer Button" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"/>
</Grid>
DockPanel
The DockPanel allows you to dock child elements to the top, bottom, left, or right edges of the available space. Any remaining space is filled by the last docked element.
Attached Properties: DockPanel.Dock with values Top, Bottom, Left, Right.
<DockPanel>
<Menu DockPanel.Dock="Top">...</Menu>
<StatusBar DockPanel.Dock="Bottom">...</StatusBar>
<Border Background="LightGray" DockPanel.Dock="Left" Width="100"/>
<Border Background="White">... (Fill) ...</Border>
</DockPanel>
Canvas
The Canvas is a simple panel that allows you to position child elements at absolute coordinates using the Canvas.Left and Canvas.Top attached properties. It does not automatically arrange elements and is typically used for custom drawing or fixed layouts.
<Canvas Background="LightYellow">
<Rectangle Fill="Blue" Canvas.Left="50" Canvas.Top="50" Width="100" Height="100"/>
<TextBlock Text="Absolute Position" Canvas.Left="180" Canvas.Top="90"/>
</Canvas>
WrapGrid
The WrapGrid arranges children in a grid-like fashion. Unlike Grid, it automatically wraps items to the next row or column when it runs out of space in the current one. It's useful for displaying a collection of items where overflow needs to be handled.
Properties: Orientation (Horizontal or Vertical), ItemWidth, ItemHeight.
<WrapGrid ItemWidth="120" ItemHeight="50" Orientation="Horizontal">
<Button Content="Item 1"/>
<Button Content="Item 2"/>
<Button Content="Item 3"/>
<Button Content="Item 4"/>
<Button Content="Item 5"/>
<Button Content="Item 6"/>
</WrapGrid>
Flexbox Concepts in WPF
While WPF doesn't have a direct "Flexbox" panel like CSS, the principles can be achieved using combinations of existing panels, especially Grid with star sizing, and StackPanel.
Key Concepts:
- Distribution: Use
Grid.ColumnDefinitionswith*sizing to distribute space. - Alignment: Use
HorizontalAlignmentandVerticalAlignmentproperties on elements. - Order: While direct reordering like CSS Flexbox is not built-in, you can achieve similar visual effects by arranging elements in the XAML or by dynamically manipulating the UI collection.
- Wrapping:
WrapGridor custom logic can simulate wrapping.
For more advanced scenarios, consider using the ItemsControl with a custom ItemsPanelTemplate or third-party libraries that might offer more direct Flexbox-like behavior.