Change Dock property in a DockPanel

Note: This external resource is published for personal reference only and is originally published here: 
http://msdn.microsoft.com/en-us/library/ms747326(VS.85).aspx



The example draws two Rectangle elements and assigns each element a Name. Two rows of Button elements represent the Dock enumeration values for each
Rectangle. The LightCoral Buttons represent the coral-colored Rectangle that is initially Docked to the Left, the LightSkyBlue Buttons represent the light blue Rectangle that is initially docked to the Right. Clicking one of these buttons raises an event handler that changes the Dock position. In addition, the text contained in the TextBlock changes to show the new docking direction for the Rectangle.

To view the complete sample, see Changing the Dock Property Sample.

<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="0,0,0,10">
<Button Click="OnClick1" Background="LightCoral">Dock = "Left"</Button>
<Button Click="OnClick2" Background="LightCoral">Dock = "Right"</Button>
<Button Click="OnClick3" Background="LightCoral">Dock = "Top"</Button>
<Button Click="OnClick4" Background="LightCoral">Dock = "Bottom"</Button>
</StackPanel>

<TextBlock DockPanel.Dock="Top" Name="Txt2">The Dock property of the LightSkyBlue Rectangle is set to Right</TextBlock>

<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="0,0,0,10">
<Button Click="OnClick5" Background="LightSkyBlue" Foreground="White">Dock = "Left"</Button>
<Button Click="OnClick6" Background="LightSkyBlue" Foreground="White">Dock = "Right"</Button>
<Button Click="OnClick7" Background="LightSkyBlue" Foreground="White">Dock = "Top"</Button>
<Button Click="OnClick8" Background="LightSkyBlue" Foreground="White">Dock = "Bottom"</Button>
</StackPanel>

<TextBlock DockPanel.Dock="Top" Name="Txt3">The LastChildFill property is set to True (default).</TextBlock>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="0,0,0,10">
<Button Click="OnClick9" Background="White">LastChildDock="True"</Button>
<Button Click="OnClick10" Background="White">LastChildDock="False"</Button>
</StackPanel>

<Border Background="LightGoldenRodYellow" BorderBrush="Black" BorderThickness="1">
<DockPanel Name="myDP">
<Rectangle Name="rect1" MinWidth="200" MinHeight="200" Stroke="Black" Fill="LightCoral" />
<Rectangle Name="rect2" MinWidth="200" MinHeight="200" Stroke="Black" Fill="LightSkyBlue" />
</DockPanel>
</Border>

The events that are defined in the preceding Extensible Application Markup Language (XAML) file are handled in a code-behind file.

public void OnClick1(object sender, RoutedEventArgs e)
{
DockPanel.SetDock(rect1, Dock.Left);
Txt1.Text = "The Dock Property of the LightCoral Rectangle is set to Left";
}