How to: Create a DockPanel


The following example creates and uses an instance of DockPanel by using code. The example shows you how to partition space by creating five Rectangle elements and positioning (docking) them inside a parent DockPanel. If you retain the default setting, the final rectangle fills all the remaining unallocated space.

            WindowTitle = "DockPanel Sample"
            'Create a DockPanel as the root Panel
            Dim myDP As New DockPanel()
            ' Add the first Rectangle to the DockPanel
            Dim rect1 As New Rectangle
            rect1.Stroke = Brushes.Black
            rect1.Fill = Brushes.SkyBlue
            rect1.Height = 25
            DockPanel.SetDock(rect1, Dock.Top)

            ' Add the second Rectangle to the DockPanel
            Dim rect2 As New Rectangle
            rect2.Stroke = Brushes.Black
            rect2.Fill = Brushes.SkyBlue
            rect2.Height = 25
            DockPanel.SetDock(rect2, Dock.Top)

            ' Add the third Rectangle to the DockPanel
            Dim rect3 As New Rectangle
            rect3.Stroke = Brushes.Black
            rect3.Fill = Brushes.Khaki
            rect3.Height = 25
            DockPanel.SetDock(rect3, Dock.Bottom)

            ' Add the fourth Rectangle to the DockPanel
            Dim rect4 As New Rectangle
            rect4.Stroke = Brushes.Black
            rect4.Fill = Brushes.PaleGreen
            rect4.Width = 200
            rect4.VerticalAlignment = VerticalAlignment.Stretch
            DockPanel.SetDock(rect4, Dock.Left)

            ' Add the fourth Rectangle to the DockPanel
            Dim rect5 As New Rectangle
            rect5.Stroke = Brushes.Black
            rect5.Fill = Brushes.White
            Me.Content = myDP

Panels Overview