WPF布局控件之DockPanel

DockPanel

DockPanel,英文释义为停靠面板,那是怎么个停靠法呢?如下:

在这里插入图片描述

<Window x:Class="LearnLayout.DockPanelWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:LearnLayout"
        mc:Ignorable="d"
        Title="DockPanelWin" Height="450" Width="800">
    <DockPanel>
		<Button Content="牛马人的一生" Background="lavender"/>
		<Button Content="我不是牛马" Background="Pink"/>
		<Button Content="下辈子还当牛马" Background="Snow"/>
		<Button Content="你才是牛马" Background="Snow"/>
	</DockPanel>
</Window>

其布局类似于水平布局的StackPanel,但是又不像,但是当我们设置其属性LastChildFill="False"后,其布局就类似StackPanel,如下:

在这里插入图片描述

<Window x:Class="LearnLayout.DockPanelWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:LearnLayout"
        mc:Ignorable="d"
        Title="DockPanelWin" Height="450" Width="800">
    <DockPanel LastChildFill="False">
		<Button Content="牛马人的一生" Background="lavender"/>
		<Button Content="我不是牛马" Background="Pink"/>
		<Button Content="下辈子还当牛马" Background="Snow"/>
		<Button Content="你才是牛马" Background="Snow"/>
	</DockPanel>
</Window>

其属性LastChildFill,字面意思,就是最后一个子空间是否填充。如果和StackPanel一样,那DockPanel的存在就没有多大意义了,可以通过设置DockPanel中控件的DockPanel.Dock属性来设置其排列顺序,如下:

在这里插入图片描述

<Window x:Class="LearnLayout.DockPanelWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:LearnLayout"
        mc:Ignorable="d"
        Title="DockPanelWin" Height="450" Width="800">
    <DockPanel >
		<Button Content="牛马人的一生" Background="lavender" DockPanel.Dock="Top"/>
		<Button Content="我不是牛马" Background="Pink" DockPanel.Dock="Left"/>
		<Button Content="下辈子还当牛马" Background="Snow" DockPanel.Dock="Bottom"/>
		<Button Content="你才是牛马" Background="Snow"/>
	</DockPanel>
</Window>

可以看到,通过设置DockPanel.Dock="Top"Content="牛马人的一生"Button布局在DockPanel的上方,其他控件同理,Content="你才是牛马"Button并未设置DockPanel.Dock,在这里也并未设置LastChildFill,所以其填充剩下的空间。
当想让第三个Button,即Content="下辈子还当牛马"的控件宽度和整个窗口一致,可以如下操作:

在这里插入图片描述

<Window x:Class="LearnLayout.DockPanelWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:LearnLayout"
        mc:Ignorable="d"
        Title="DockPanelWin" Height="450" Width="800">
    <DockPanel >
		<Button Content="牛马人的一生" Background="lavender" DockPanel.Dock="Top"/>
		<Button Content="下辈子还当牛马" Background="Snow" DockPanel.Dock="Bottom"/>
		<Button Content="我不是牛马" Background="Pink" DockPanel.Dock="Left"/>
		<Button Content="你才是牛马" Background="Snow"/>
	</DockPanel>
</Window>

在这里,仅仅是将Button位置调换,即可完成,因此可以得知DockPanel中是根据控件顺序将控件填满DockPanel的。
另外,当改变窗口尺寸时,这些控件也会对应比例改变,如下:

启动窗口不操作:

在这里插入图片描述

启动后拉长窗口:

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值