XAML - DockPanel

DockPanel defines an area to arrange child elements relative to each other, either horizontally or vertically. With DockPanel, you can easily dock child elements to top, bottom, right, left, and center with Dock property.
With LastChildFill property, the last child element fills the remaining space regardless of any other dock value when set for that element. The hierarchical inheritance of DockPanel 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.)
Dock
Gets or sets a value that indicates the position of a child element within a parent DockPanel.
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.
LastChildFill
Gets or sets a value that indicates whether the last child element within a DockPanel stretches to fill the remaining available space.
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.)
GetDock
Gets the value of the Dock attached property for a specified UIElement.
SetDock
Sets the value of the Dock attached property to a specified element.
The following example shows how to add child elements into a DockPanel. Here is the XAML implementation to create buttons inside a DockPanel.
<Window x:Class = "XAMLDockPanel.Window1" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "300" Width = "604">
	
   <Grid>....
      <DockPanel LastChildFill = "True"> 
         <Button Content = "Top" DockPanel.Dock = "Top" Click = "Click_Me" /> 
         <Button Content = "Bottom" DockPanel.Dock = "Bottom" Click = "Click_Me" /> 
         <Button Content = "Left" Click = "Click_Me" />
         <Button Content = "Right" DockPanel.Dock = "Right" Click = "Click_Me" /> 
         <Button Content = "Center" Click = "Click_Me" /> 
      </DockPanel> 
   </Grid> 
	
</Window> 
Given below is the implementation in C# for event −
using System; 
using System.Windows; 
using System.Windows.Controls; 
 
namespace XAMLDockPanel { 
   /// <summary> 
      /// Interaction logic for Window1.xaml 
   /// </summary> 
	
   public partial class Window1 : Window { 
      public Window1() { 
         InitializeComponent(); 
      }
      private void Click_Me(object sender, RoutedEventArgs e) { 
         Button btn = sender as Button; 
         string str = btn.Content.ToString() + " button clicked" ; 
         MessageBox.Show(str); 
      } 
   }
}
When you compile and execute the above code, it will produce the following output −
On clicking any button, it will also display a message. For example, when you click the button that is at Center, it will display the following message −
We recommend you to execute the above example code and experiment with some other properties as well.
 Print
 Add Notes
 Bookmark this page