Using WPF DockPanel to dock child elements

The DockPanel allows you to dock the child elements to any one of the four sides (top, right, bottom, or left). By default, the last element (if not given any specific dock position) fills the remaining space. You can use it when you need to divide your window to regions and/or use when you want to place one or more elements to any of the sides.

As shown in the following code snippet, you need to set the DockPanel.Dock attribute of the child elements to one of the four values:

  <DockPanel Background="LightCyan"> 
    <Border Width="150" Height="50"  
            DockPanel.Dock="Bottom" Background="YellowGreen"> 
      <TextBlock Text="Bottom Docking"  
                 HorizontalAlignment="Center"  
                 VerticalAlignment="Center"/> 
    </Border> 
    <Border Width="150" Height="50"  
            DockPanel.Dock="Top" Background="YellowGreen"> 
      <TextBlock Text="Top Docking"  
                 HorizontalAlignment="Center"  
                 VerticalAlignment="Center"/> 
    </Border> 
    <Border Width="150" Height="50"  
            DockPanel.Dock="Right" Background="YellowGreen"> 
      <TextBlock Text="Right Docking"  
                 HorizontalAlignment="Center"  
                 VerticalAlignment="Center"/> 
    </Border> 
    <Border Width="150" Height="50"  
            DockPanel.Dock="Left" Background="YellowGreen"> 
      <TextBlock Text="Left Docking"  
                 HorizontalAlignment="Center"  
                 VerticalAlignment="Center"/> 
    </Border> 
    <Border Width="150" Height="50"  
            Background="White"> 
      <TextBlock Text="No Docking"  
                 HorizontalAlignment="Center"  
                 VerticalAlignment="Center"/> 
    </Border> 
  </DockPanel> 

When you run the preceding example, the result will look like the following diagram, positioning all the child elements based on the value specified as the DockPanel.Dock attribute:

Note that the last element, which does not have any docking position set, has taken the remaining space and is placed at the center position of the Window.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.153.38