- WPF Tutorial
- WPF - Home
- WPF - Overview
- WPF - Environment Setup
- WPF - Hello World
- WPF - XAML Overview
- WPF - Elements Tree
- WPF - Dependency Properties
- WPF - Routed Events
- WPF - Controls
- WPF - Layouts
- WPF - Nesting Of Layout
- WPF - Input
- WPF - Command Line
- WPF - Data Binding
- WPF - Resources
- WPF - Templates
- WPF - Styles
- WPF - Triggers
- WPF - Debugging
- WPF - Custom Controls
- WPF - Exception Handling
- WPF - Localization
- WPF - Interaction
- WPF - 2D Graphics
- WPF - 3D Graphics
- WPF - Multimedia
- WPF Useful Resources
- WPF - Quick Guide
- WPF - Useful Resources
- WPF - Discussion
WPF - StackPanel
Stack panel is a simple and useful layout panel in XAML. In stack panel, child elements can be arranged in a single line, either horizontally or vertically, based on the orientation property. It is often used whenever any kind of list is to be created. The hierarchical inheritance of StackPanel class is as follows −
Commonly Used Properties of StackPanel
Sr. No. | Property & Description |
---|---|
1 | Background Gets or sets a Brush that fills the panel content area. (Inherited from Panel) |
2 | Children Gets a UIElementCollection of child elements of this Panel. (Inherited from Panel.) |
3 | Height Gets or sets the suggested height of the element. (Inherited from FrameworkElement.) |
4 | ItemHeight Gets or sets a value that specifies the height of all items that are contained within a WrapPanel. |
5 | ItemWidth Gets or sets a value that specifies the width of all items that are contained within a WrapPanel. |
6 | LogicalChildren Gets an enumerator that can iterate the logical child elements of this Panel element. (Inherited from Panel.) |
7 | LogicalOrientation The Orientation of the panel, if the panel supports layout in only a single dimension. (Inherited from Panel.) |
8 | Margin Gets or sets the outer margin of an element. (Inherited from FrameworkElement.) |
9 | Name Gets or sets the identifying name of the element. The name provides a reference so that code-behind, such as event handler code, can refer to a markup element after it is constructed during processing by a XAML processor. (Inherited from FrameworkElement.) |
10 | Orientation Gets or sets a value that specifies the dimension in which child content is arranged. |
11 | Parent Gets the logical parent element of this element. (Inherited from FrameworkElement.) |
12 | Resources Gets or sets the locally-defined resource dictionary. (Inherited from FrameworkElement.) |
13 | Style Gets or sets the style used by this element when it is rendered. (Inherited from FrameworkElement.) |
14 | Width Gets or sets the width of the element. (Inherited from FrameworkElement.) |
Example
The following example shows how to add child elements into a StackPanel. The following XAML implementation creates buttons inside a StackPanel with some properties.
<Window x:Class = "WPFStackPanel.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local = "clr-namespace:WPFStackPanel" mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> <Grid> <StackPanel Orientation = "Horizontal"> <Button x:Name = "button" Content = "Button" Margin = "10" Width = "120" Height = "30" /> <Button x:Name = "button1" Content = "Button" Margin = "10" Width = "120" Height = "30" /> <Button x:Name = "button2" Content = "Button" Margin = "10" Width = "120" Height = "30" /> <Button x:Name = "button3" Content = "Button" Margin = "10" Width = "120" Height = "30" /> </StackPanel> </Grid> </Window>
When you compile and execute the above code, it will produce the following window. You can see that the child elements are arranged in horizontal order. Yan can change the arrangement by setting the orientation property to Horizontal. By default, child elements will be arranged in vertical order.
We recommend that you execute the above example code and try the other properties of this class as well.