DockPanel很适合对窗口进行一些个性化的布局,它可以对未分配的区域,指定位置进行放置。
在DockPanel中可以很容易地停靠在所有四个方向的内容(上,下,左,右)。这使它在许多情况下成为一个很好的选择,您希望将窗口划分为特定区域,特别是因为默认情况下,DockPanel 内的最后一个元素,除非此功能被明确禁用,否则将自动填充其余空间(中心)。
我们在 WPF 中的许多其他面板中看到的那样,您可以通过使用面板的附加属性开始利用面板的可能性,在本例中为 DockPanel.Dock 属性,它决定了您希望子控件朝哪个方向移动停靠到。如果您不使用它,第一个控件将停靠在左侧,最后一个占用剩余空间。
<DockPanel LastChildFill="False">
<Button Content="button1" DockPanel.Dock="Top"/>
<Button Content="button2" DockPanel.Dock="Bottom"/>
<Button Content="button3" DockPanel.Dock="Left"/>
<Button Content="button4" DockPanel.Dock="Right"/>
<Button Content="button5" />
<Button Content="button6" />
</DockPanel>