• WPF布局控件之DockPanel布局


    前言:博主文章仅用于学习、研究和交流目的,不足和错误之处在所难免,希望大家能够批评指出,博主核实后马上更改。

    概述:

    DockPanel 位置子控件基于子 Dock 属性,你有 4 个选项停靠,左 (默认) ,右,上,下。 如果希望添加到 DockPanel 的最后一项填充剩余空间,可以将 DockPanel LastChildFill 属性设置为 true。

    名称说明
    Grid网格,根据自定义行和列来设置控件的布局
    StackPanel栈式面板,包含的元素在竖直或水平方向排成一条直线
    WrapPanel自动折行面板,包含的元素在排满一行后,自动换行
    DockPanel泊靠式面板,内部的元素可以选择泊靠方向
    UniformGrid网格,UniformGrid就是Grid的简化版,每个单元格的大小相同。
    Canvas画布,内部元素根据像素为单位绝对坐标进行定位
    Border装饰的控件,此控件用于绘制边框及背景,在Border中只能有一个子控件

    一、DockPanel

    常用属性数据类型可选值说明
    DockPanelDockLeft、Top、Right、Bottom
    MarginThickness获取或设置元素的外边距
    HorizontalAlignmentHorizontalAlignmentCenter(中心)/Left(靠左)/Right(靠右)/Stretch(拉伸以填充父元素)决定内部元素在水平方向的对齐方式
    VerticalAlignmentVerticalAlignmentTop(上方)/Center(中心)/Bottom(下方)/Stretch(拉伸以填充父元素)决定内部元素在垂直方向的对齐方式
    Opacitydouble透明度
    LastChildFillbool获取或设置一个值,该值指示 DockPanel 中的最后一个子元素是否拉伸以填充剩余的可用空间,默认为True(填充)

    LastChildFill=“True” 默认全部填充

     <DockPanel LastChildFill="True">
         <Button DockPanel.Dock="Top" Content="Button Top"/>
         <Button DockPanel.Dock="Left"  Content="ButtonLeft"/>
         <Button DockPanel.Dock="Right" Content="Button Right"/>
         <Button DockPanel.Dock="Bottom" Content="Button Bottom"/>
         <Button Content="Button Center"/>
     DockPanel>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    LastChildFill=“False”

    <DockPanel LastChildFill="False">
        <Button DockPanel.Dock="Top" Content="Button Top"/>
        <Button DockPanel.Dock="Left"  Content="ButtonLeft"/>
        <Button DockPanel.Dock="Right" Content="Button Right"/>
        <Button DockPanel.Dock="Bottom" Content="Button Bottom"/>
        <Button Content="Button Center"/>
    DockPanel>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    Opacity=“0.1”

     <DockPanel  Opacity="0.1">
         <Button DockPanel.Dock="Top" Content="Button Top"/>
         <Button DockPanel.Dock="Left"  Content="ButtonLeft"/>
         <Button DockPanel.Dock="Right" Content="Button Right"/>
         <Button DockPanel.Dock="Bottom" Content="Button Bottom"/>
         <Button Content="Button Center"/>
     DockPanel>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    Margin=“20”

    <DockPanel Margin="20">
        <Button DockPanel.Dock="Top" Content="Button Top"/>
        <Button DockPanel.Dock="Left"  Content="ButtonLeft"/>
        <Button DockPanel.Dock="Right" Content="Button Right"/>
        <Button DockPanel.Dock="Bottom" Content="Button Bottom"/>
        <Button Content="Button Center"/>
    DockPanel>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    总结

    在实际工作中,我们可以使用DockPanel、HorizontalAlignment、VerticalAlignment,LastChildFill 这四个个属性组合各种排列和对齐方式。

  • 相关阅读:
    个人笔记--python用tanh画圆形,正方形,长方形(epsilon界面宽度)
    一种通过篡改特定代码数据修复嵌入式产品BUG的方法
    十大排序算法C++实现
    设计模型之六大原则(有的地方称之为七大原则)
    Redis配置优化
    ROS2——通信接口(十)
    如何通过WinDbg获取方法参数值
    java 类和对象(方法与方法重载)
    Ultipa Transporter V4.3.22 即将发布,解锁更多易用功能!
    『现学现忘』Git基础 — 35、Git中删除文件
  • 原文地址:https://blog.csdn.net/qq_36819973/article/details/134200924