Como: Change Dock Properties
O exemplo a seguir mostra como alterar o valor da propriedade Dock de DockPanel. Este exemplo também demonstra a funcionalidade dos valores Dock diferentes.
Exemplo
O exemplo desenha dois elementos Rectangle e atribui cada elemento a um Name. Duas linhas de elementos de Button representam os valores da enumeração Dock para cada Rectangle. Os LightCoral Buttons representam as cores corais Rectangle que são inicialmente Docked para a Left, aLightSkyBlue Buttons representam a luz azul Rectangle que são inicialmente encaixadas para a Right. Clicando em um desses botões gera um manipulador de eventos que altera a posição de Dock. Além disso, o texto contido na TextBlock altera para mostrar a nova direção de encaixe para o Rectangle.
To view the complete sample, see Exemplo de propriedade de encaixe.
<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>
Os eventos que são definidos no arquivo Extensible Application Markup Language (XAML) anterior são tratados em um arquivo de código de apoio.
Private Sub OnClick1(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
System.Windows.Controls.DockPanel.SetDock(rect1, System.Windows.Controls.Dock.Left)
Txt1.Text = "The Dock property of the LightCoral Rectangle is set to Left"
End Sub
private void OnClick1(object sender, RoutedEventArgs e)
{
DockPanel.SetDock(rect1, Dock.Left);
Txt1.Text = "The Dock Property of the LightCoral Rectangle is set to Left";
}