XAML - StackPanel

Stack panel is a simple and useful layout panel in XAML. In a 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 needs to be created. Stack panels are used by ItemsControls like Menu, ListBox, and ComboBox. The hierarchical inheritance of StackPanel class is as follows −
Background
Gets or sets a Brush that fills the panel content area. (Inherited from Panel)
Children
Gets a UIElementCollection of child elements of this Panel. (Inherited from Panel.)
Height
Gets or sets the suggested height of the element. (Inherited from FrameworkElement.)
ItemHeight
Gets or sets a value that specifies the height of all items that are contained within a WrapPanel.
ItemWidth
Gets or sets a value that specifies the width of all items that are contained within a WrapPanel.
LogicalChildren
Gets an enumerator that can iterate the logical child elements of this Panel element. (Inherited from Panel.)
LogicalOrientation
The Orientation of the panel, if the panel supports layout in only a single dimension. (Inherited from Panel.)
Margin
Gets or sets the outer margin of an element. (Inherited from FrameworkElement.)
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.)
Orientation
Gets or sets a value that specifies the dimension in which child content is arranged.
Parent
Gets the logical parent element of this element. (Inherited from FrameworkElement.)
Resources
Gets or sets the locally-defined resource dictionary. (Inherited from FrameworkElement.)
Style
Gets or sets the style used by this element when it is rendered. (Inherited from FrameworkElement.)
Width
Gets or sets the width of the element. (Inherited from FrameworkElement.)
The following example shows how to add child elements into a StackPanel. Here is the XAML implementation in which Ellipses are created inside a StackPanel which some properties.
<Window x:Class = "XAMLStackPanel.Window1" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "400" Width = "604"> 
	
   <Grid> 
      <StackPanel x:Name = "myPanel"> 
         <Button Content = "Button 1" Width = "100" Height = "40" Margin = "10" /> 
         <Button Content = "Button 2" Width = "100" Height = "40" Margin = "10" /> 
         <RadioButton Content = "Radio Button 1" Width = "100"  Margin = "10" /> 
         <RadioButton Content = "Radio Button 2" Width = "100"  Margin = "10" /> 
         <RadioButton Content = "Radio Button 3" Width = "100"  Margin = "10" /> 
         <CheckBox Content = "Check Box 1" Width = "100" Margin = "10" /> 
         <CheckBox Content = "Check Box 2" Width = "100" Margin = "10" />
      </StackPanel> 
   </Grid> 
	
</Window> 
When you compile and execute the above code, it will produce the following output. You can see that, by default, the child elements are arranged in a Vertical order. Yan can change the arrangement by setting the orientation property to Horizontal.
We recommend you to execute the above example code and experiment with some other properties as well.
 Print
 Add Notes
 Bookmark this page