In Windows Presentation Foundation (WPF), several types of panels are available to arrange and layout UI elements. Here are the main types:
single line
(either vertically or horizontally).simple lists of controls
.order from left to right
, breaking content to the next line
at the edge of the containing box.top, bottom, left, right, or center
.rows and columns
.absolute positioning
.StackPanel but supports virtualization
, which improves performance by only creating UI elements for visible items.RowDefinition
and ColumnDefinition
elements within the Grid.Grid.RowSpan
and
properties.<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="Grid Example" Height="200" Width="300"><Grid><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/><RowDefinition Height="*"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><TextBlock Text="Name:" Grid.Row="0" Grid.Column="0" Margin="5"/><TextBox Grid.Row="0" Grid.Column="1" Margin="5"/><TextBlock Text="Address:" Grid.Row="1" Grid.Column="0" Margin="5"/><TextBox Grid.Row="1" Grid.Column="1" Margin="5"/><Button Content="Submit" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="5"/></Grid></Window>
vertical
orientation stacks its children from top to bottom
. This is the default orientation.horizontal
orientation stacks its children from left to right
. You can set the orientation using the Orientation property.<StackPanel Orientation="Horizontal"><!-- Child elements --></StackPanel>
A DataTemplate in WPF is a powerful feature that allows you to define the visual representation of data
objects
. This is especially useful when working with collections of data in controls like
ListBox, ListView, ComboBox, or ItemsControl
. By using a DataTemplate, you can control how each data
item appears in these controls, separating the presentation from the data logic.
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="DataTemplate Example" Height="200" Width="300"><Window.Resources><DataTemplate x:Key="PersonTemplate"><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Name}" Margin="5"/><TextBlock Text="{Binding Age}" Margin="5"/></StackPanel></DataTemplate></Window.Resources><Grid><ListBox x:Name="personListBox" ItemTemplate="{StaticResource PersonTemplate}" /></Grid></Window>
public partial class MainWindow : Window{public MainWindow(){InitializeComponent();List<Person> people = new List<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 },new Person { Name = "Charlie", Age = 35 }};personListBox.ItemsSource = people;}}
An ItemsControl in WPF is a base class for all controls that can contain a collection of items
, such as
ListBox, ListView, ComboBox, and TreeView
. It provides a flexible way to display a list of data items
and offers various features to customize how the items are presented.
ItemsSource
property is used to bind the control to a collection of data items. This can be any
collection that implements IEnumerable, such as List, ObservableCollection, or even arrays
.ItemTemplate
property allows you to define a DataTemplate that specifies the visual structure
of each item in the collection. This is essential for customizing the appearance of the items.ItemsPanel
property allows you to specify the layout panel that will be used to arrange the items.
By default, ItemsControl uses a StackPanel with vertical orientation. You can change this to other panels
like WrapPanel or Grid.ItemContainerStyle
property allows you to apply a style to each item container. This can be used
to set properties such as margin, padding, or background color for each item.public class Person{public string Name { get; set; }public int Age { get; set; }}
public class MainViewModel{public ObservableCollection<Person> People { get; set; }public MainViewModel(){People = new ObservableCollection<Person>{new Person { Name = "Alice", Age = 30 },new Person { Name = "Bob", Age = 25 },new Person { Name = "Charlie", Age = 35 }};}}
<Window x:Class="WpfApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="ItemsControl Example" Height="200" Width="300"><Window.DataContext><local:MainViewModel /></Window.DataContext><Grid><ItemsControl ItemsSource="{Binding People}"><ItemsControl.ItemTemplate><DataTemplate><StackPanel><TextBlock Text="{Binding Name}" /><TextBlock Text="{Binding Age}" /></StackPanel></DataTemplate></ItemsControl.ItemTemplate><ItemsControl.ItemsPanel><ItemsPanelTemplate><WrapPanel /></ItemsPanelTemplate></ItemsControl.ItemsPanel><ItemsControl.ItemContainerStyle><Style><Setter Property="Control.Margin" Value="5" /><Setter Property="Control.Background" Value="LightGray" /></Style></ItemsControl.ItemContainerStyle></ItemsControl></Grid></Window>
Quick Links
Legal Stuff
Social Media