WPF基础之UI布局(wpf ui界面设计)
liuian 2025-06-03 23:29 16 浏览
知识点:
WPF中的布局控件主要有以下几种:
StackPanel:栈面板,可以将元素排列成一行或者一列。其特点是:每个元素各占一行或者一列。
WrapPanel:环绕面板,将各个控件从左至右按照行或列的顺序罗列,当长度或高度不够时就会自动调整进行换行,后续排序按照从上至下或从左至右的顺序进行。
DockPanel:停靠面板,定义一个区域,在此区域中,您可以使子元素通过描点的形式排列,这些对象位于 Children 属性中。
Grid:网格面板, Grid顾名思义就是“网格”,以表格形式布局元素,对于整个面板上的元素进行布局,它的子控件被放在一个一个事先定义好的小格子里面,整齐配列。 Grid和其他各个Panel比较起来,功能最多也最为复杂。
Canvas:画布面板,画布,用于完全控制每个元素的精确位置。他是布局控件中最为简单的一种,直接将元素放到指定位置,主要来布置图面。
1、StackPanel
Orientation属性指定排列方式:
Vertical(垂直)【默认】,垂直排列时,每个元素都与面板一样宽。
Horizontal(水平),水平排列时,每个元素都与面板一样高;
如果包含的元素超过了面板空间,它只会截断多出的内容。 元素的Margin属性用于使元素之间产生一定得间隔,当元素空间大于其内容的空间时,剩余空间将由HorizontalAlignment和 VerticalAlignment属性来决定如何分配。
首先是【默认】垂直(Vertical)情况下的布局:
<Window
x:Class="LayoutSample.StackPanelSample"
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:local="clr-namespace:LayoutSample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="StackPanelSample"
Width="800"
Height="450"
mc:Ignorable="d">
<StackPanel Margin="5,5,5,5">
<Button
x:Name="button1"
Margin="2,5,2,5"
Content="按钮1" />
<Button
x:Name="button2"
Margin="2,5,2,5"
Content="按钮2" />
<Button
x:Name="button3"
Margin="2,5,2,5"
Content="按钮3" />
<Button
x:Name="button4"
Margin="2,5,2,5"
Content="按钮4" />
<Button
x:Name="button5"
Margin="2,5,2,5"
Content="按钮5" />
<Button
x:Name="button6"
Margin="2,5,2,5"
Content="按钮6" />
<Button
x:Name="button7"
Margin="2,5,2,5"
Content="按钮7" />
<Button
x:Name="button8"
Margin="2,5,2,5"
Content="按钮8" />
</StackPanel>
</Window>
水平布局,示例代码,与垂直排列相同,只是多了一个Oriention属性:
<StackPanel Margin="5,5,5,5" Orientation="Horizontal">
StackPanel其他常用属性:
HorizontalAlignment:水平对齐方式,值:Left,Center,Right,Stretch。
VerticalAlignment:垂直对齐方式,值:Bottom , Center,Stretch,Top。
Visibility:设置StackPanel是否可见,值:Visible,Hidden,Collapsed。
Background:设置背景颜色,值为Bursh类型的枚举值。
Width,Height,MinWidth,MinHeight,MaxWidth,MaxHeight:分别用来设置StackPanel的宽,高,最小宽,最小高,最大宽,最大高。
WrapPanel【环绕面板】:
WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列,当长度或高度不够时就会自动调整进行换行,后续排序按照从上至下或从左至右的顺序进行。
水平排列:当Orientation属性的值设置为 Horizontal:元素是从左向右排列的,然后自上至下自动换行。
<Window
x:Class="LayoutSample.WrapPanelSample"
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:local="clr-namespace:LayoutSample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="WrapPanelSample"
Width="400"
Height="350"
mc:Ignorable="d">
<WrapPanel>
<Button
x:Name="button1"
Margin="2,5,2,5"
Content="按钮1" />
<Button
x:Name="button2"
Margin="2,5,2,5"
Content="按钮2" />
<Button
x:Name="button3"
Margin="2,5,2,5"
Content="按钮3" />
<Button
x:Name="button4"
Margin="2,5,2,5"
Content="按钮4" />
<Button
x:Name="button5"
Margin="2,5,2,5"
Content="按钮5" />
<Button
x:Name="button6"
Margin="2,5,2,5"
Content="按钮6" />
<Button
x:Name="button7"
Margin="2,5,2,5"
Content="按钮7" />
<Button
x:Name="button8"
Margin="2,5,2,5"
Content="按钮8" />
</WrapPanel>
</Window>
垂直布局,示例代码,与水平排列相同,只是多了一个Oriention属性:
<WrapPanel Margin="5,5,5,5" Orientation="Vertical">
WrapPanel其他常用属性,除了StackPanel属性常用属性,还有两个,如下所示:
ItemHeight:所有的元素都相同高度。
ItemWidth:所有的元素都相同宽度。
DockPanel【停靠面板】:
停靠面板类似于WinForm中控件的Dock属性。DockPanel会对每个子元素进行排序,并将根据指定的边进行停靠,多个停靠在同侧的元素则按顺序排序。
默认情况下,所有子元素都是靠左停靠【DockPanel.Dock=Left】
<Window
x:Class="LayoutSample.DockPanelSample"
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:local="clr-namespace:LayoutSample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="DockPanelSample"
Width="600"
Height="450"
mc:Ignorable="d">
<DockPanel Margin="5,5,5,5">
<Button
x:Name="button1"
Margin="2,5,2,5"
Content="按钮1"
DockPanel.Dock="Left" />
<Button
x:Name="button2"
Margin="2,5,2,5"
Content="按钮2"
DockPanel.Dock="Top" />
<Button
x:Name="button3"
Margin="2,5,2,5"
Content="按钮3"
DockPanel.Dock="Right" />
<Button
x:Name="button4"
Margin="2,5,2,5"
Content="按钮4"
DockPanel.Dock="Bottom" />
<Button
x:Name="button5"
Margin="2,5,2,5"
Content="按钮5"
DockPanel.Dock="Left" />
<Button
x:Name="button6"
Margin="2,5,2,5"
Content="按钮6"
DockPanel.Dock="Top" />
<Button
x:Name="button7"
Margin="2,5,2,5"
Content="按钮7"
DockPanel.Dock="Right" />
<Button
x:Name="button8"
Margin="2,5,2,5"
Content="按钮8"
DockPanel.Dock="Bottom" />
<Button
x:Name="button9"
Margin="2,5,2,5"
Content="按钮9" />
</DockPanel>
</Window>
DockPanel其他常用属性,除了StackPanel属性常用属性,还有一个,如下所示:
- LastChildFill:最后一个子元素,是否填充剩余空间,默认为True。
设置不顶边:
<DockPanel Margin="5,5,5,5" LastChildFill="False">
Grid【网格面板】:
以表格形式布局元素,对于整个面板上的元素进行布局,要使用Grid,首先要向RowDefinitions和ColumnDefinitions属性中添加一定数量的RowDefinitions和 ColumnDefinitions元素,从而定义行数和列数。
放置在Grid面板中的控件元素都必须显示采用Row和Column附加属性定义其放置所在的行和列,这两个属性的值都是从0开始的索引数,如果没有显式设置任何行或列,Grid将会隐式地将控件加入在第0行第0列。
默认情况下,如果不设置Grid面板的行列宽,与高,则默认均分,且如果不显示设置子元素的大小,则默认填充。
<Window
x:Class="LayoutSample.GridSample"
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:local="clr-namespace:LayoutSample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="GridSample"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
x:Name="button1"
Grid.Row="0"
Grid.Column="0"
Margin="2,5,2,5"
Content="按钮1" />
<Button
x:Name="button2"
Grid.Row="0"
Grid.Column="1"
Margin="2,5,2,5"
Content="按钮2" />
<Button
x:Name="button3"
Grid.Row="0"
Grid.Column="2"
Margin="2,5,2,5"
Content="按钮3" />
<Button
x:Name="button4"
Grid.Row="1"
Grid.Column="0"
Margin="2,5,2,5"
Content="按钮4" />
<Button
x:Name="button5"
Grid.Row="1"
Grid.Column="1"
Margin="2,5,2,5"
Content="按钮5" />
<Button
x:Name="button6"
Grid.Row="1"
Grid.Column="2"
Margin="2,5,2,5"
Content="按钮6" />
<Button
x:Name="button7"
Grid.Row="2"
Grid.Column="0"
Margin="2,5,2,5"
Content="按钮7" />
<Button
x:Name="button8"
Grid.Row="2"
Grid.Column="1"
Margin="2,5,2,5"
Content="按钮8" />
<Button
x:Name="button9"
Grid.Row="2"
Grid.Column="2"
Margin="2,5,2,5"
Content="按钮9" />
</Grid>
</Window>
列宽和行高,分别可以在ColumnDefinition、RowDefinition里面指定Width、Height的值。
设置宽与高的几种方式:
固定值:设置具体的数字,单位为像素px。
Auto:根据子元素的大小,自动分配大小,刚好能够容纳子元素的内容。
星号*:根据比例自动分配剩余空间。
Grid面板其他属性,除了StackPanel属性常用属性,还有两个:
Grid.ColumnSpan:用于设置元素跨越的单元格列数。
Grid.RowSpan:用于设置元素跨越的单元格行数。
Canvas【画布面板】:
画布,用于完全控制每个元素的精确位置。他是布局控件中最为简单的一种,直接将元素放到指定位置,主要来布置图面。
Canvas通过设置Left,Top,Bottom,Right等属性的值,来设置子元素的位置,如果同时设置Canvas.Left和Canvas.Right属性,那么后者将会被忽略
<Canvas>
<Button x:Name="button1" Margin="2 5 2 5" Content="按钮1" Canvas.Left="20" Canvas.Top="50"/>
</Canvas>
Canvas的其他属性,除了StackPanel的常用属性外,还有两个属性:
- ClipToBounds:超出边界部分,是否需要进行剪裁。
- Panel.ZIndex:用于设置子元素在Z方向上的顺序,值越大,越靠上。如果两个元素重叠,则ZIndex值小的将会被覆盖。
相关推荐
- 总结下SpringData JPA 的常用语法
-
SpringDataJPA常用有两种写法,一个是用Jpa自带方法进行CRUD,适合简单查询场景、例如查询全部数据、根据某个字段查询,根据某字段排序等等。另一种是使用注解方式,@Query、@Modi...
- 解决JPA在多线程中事务无法生效的问题
-
在使用SpringBoot2.x和JPA的过程中,如果在多线程环境下发现查询方法(如@Query或findAll)以及事务(如@Transactional)无法生效,通常是由于S...
- PostgreSQL系列(一):数据类型和基本类型转换
-
自从厂子里出来后,数据库的主力就从Oracle变成MySQL了。有一说一哈,贵确实是有贵的道理,不是开源能比的。后面的工作里面基本上就是主MySQL,辅MongoDB、ES等NoSQL。最近想写一点跟...
- 基于MCP实现text2sql
-
目的:基于MCP实现text2sql能力参考:https://blog.csdn.net/hacker_Lees/article/details/146426392服务端#选用开源的MySQLMCP...
- ORACLE 错误代码及解决办法
-
ORA-00001:违反唯一约束条件(.)错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。ORA-00017:请求会话以设置跟踪事件ORA-00018:超出最大会话数ORA-00...
- 从 SQLite 到 DuckDB:查询快 5 倍,存储减少 80%
-
作者丨Trace译者丨明知山策划丨李冬梅Trace从一开始就使用SQLite将所有数据存储在用户设备上。这是一个非常不错的选择——SQLite高度可靠,并且多种编程语言都提供了广泛支持...
- 010:通过 MCP PostgreSQL 安全访问数据
-
项目简介提供对PostgreSQL数据库的只读访问功能。该服务器允许大型语言模型(LLMs)检查数据库的模式结构,并执行只读查询操作。核心功能提供对PostgreSQL数据库的只读访问允许L...
- 发现了一个好用且免费的SQL数据库工具(DBeaver)
-
缘起最近Ai不是大火么,想着自己也弄一些开源的框架来捣腾一下。手上用着Mac,但Mac都没有显卡的,对于学习Ai训练模型不方便,所以最近新购入了一台4090的拯救者,打算用来好好学习一下Ai(呸,以上...
- 微软发布.NET 10首个预览版:JIT编译器再进化、跨平台开发更流畅
-
IT之家2月26日消息,微软.NET团队昨日(2月25日)发布博文,宣布推出.NET10首个预览版更新,重点改进.NETRuntime、SDK、libraries、C#、AS...
- 数据库管理工具Navicat Premium最新版发布啦
-
管理多个数据库要么需要使用多个客户端应用程序,要么找到一个可以容纳你使用的所有数据库的应用程序。其中一个工具是NavicatPremium。它不仅支持大多数主要的数据库管理系统(DBMS),而且它...
- 50+AI新品齐发,微软Build放大招:拥抱Agent胜算几何?
-
北京时间5月20日凌晨,如果你打开微软Build2025开发者大会的直播,最先吸引你的可能不是一场原本属于AI和开发者的技术盛会,而是开场不久后的尴尬一幕:一边是几位微软员工在台下大...
- 揭秘:一条SQL语句的执行过程是怎么样的?
-
数据库系统能够接受SQL语句,并返回数据查询的结果,或者对数据库中的数据进行修改,可以说几乎每个程序员都使用过它。而MySQL又是目前使用最广泛的数据库。所以,解析一下MySQL编译并执行...
- 各家sql工具,都闹过哪些乐子?
-
相信这些sql工具,大家都不陌生吧,它们在业内绝对算得上第一梯队的产品了,但是你知道,他们都闹过什么乐子吗?首先登场的是Navicat,这款强大的数据库管理工具,曾经让一位程序员朋友“火”了一把。Na...
- 详解PG数据库管理工具--pgadmin工具、安装部署及相关功能
-
概述今天主要介绍一下PG数据库管理工具--pgadmin,一起来看看吧~一、介绍pgAdmin4是一款为PostgreSQL设计的可靠和全面的数据库设计和管理软件,它允许连接到特定的数据库,创建表和...
- Enpass for Mac(跨平台密码管理软件)
-
还在寻找密码管理软件吗?密码管理软件有很多,但是综合素质相当优秀且完全免费的密码管理软件却并不常见,EnpassMac版是一款免费跨平台密码管理软件,可以通过这款软件高效安全的保护密码文件,而且可以...
- 一周热门
-
-
Python实现人事自动打卡,再也不会被批评
-
【验证码逆向专栏】vaptcha 手势验证码逆向分析
-
Psutil + Flask + Pyecharts + Bootstrap 开发动态可视化系统监控
-
一个解决支持HTML/CSS/JS网页转PDF(高质量)的终极解决方案
-
再见Swagger UI 国人开源了一款超好用的 API 文档生成框架,真香
-
网页转成pdf文件的经验分享 网页转成pdf文件的经验分享怎么弄
-
C++ std::vector 简介
-
飞牛OS入门安装遇到问题,如何解决?
-
系统C盘清理:微信PC端文件清理,扩大C盘可用空间步骤
-
10款高性能NAS丨双十一必看,轻松搞定虚拟机、Docker、软路由
-
- 最近发表
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)