WPF中的DockPanel布局
开发工具与关键技术:Visual Studio 2015、WPF
作者:黄元进
撰写时间:2019年4月14日
DockPanel定义一个区域,在此区域中,你可以使子元素通过描点的形式排列,这些对象位于Children属性中。DockPanel会对每个子元素进行排序,并将根据指定的边进行停靠,多个停靠在同侧的元素则按顺序排序。使用XAML代码实现如下:
<Window x:Class="DockPanel布局.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>
<Button DockPanel.Dock="Bottom" Content="yi" Height="30"></Button>
<Button DockPanel.Dock="Left" Content="er" Width="30"></Button>
<Button DockPanel.Dock="Right" Content="san" Width="30"></Button>
<Button DockPanel.Dock="Top" Content="si" Height="30"></Button>
<StackPanel>
<TextBlock Width="50">44444</TextBlock>
</StackPanel>
</DockPanel>
</Window>
实现效果:
在DockPanel中,指定停靠边的控件,会根据定义的顺序占领边角,所有控件绝不会重叠。