WPF2022终结版系列课程笔记 1 WPF 基本布局

2024-04-22 19:04

本文主要是介绍WPF2022终结版系列课程笔记 1 WPF 基本布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本笔记为B站
微软系列技术教程 WPF项目实战合集(2022终结版) 项目记录

WPF 基本布局

WPF布局原则

一个窗口中只能包含一个元素
不应显示设置元素尺寸
不应使用坐标设置元素的位置
可以嵌套布局容器

WPF布局容器

StackPanel: 水平或垂直排列元素、Orientation属性分别: Horizontal / Vertical

WrapPanel : 水平或垂直排列元素、针对剩余空间不足会进行换行或换列进行排列

DockPanel : 根据容器的边界、元素进行Dock.Top、Left、Right、Bottom设置

Grid : 类似table表格、可灵活设置行列并放置控件元素、比较常用

UniformGrid : 指定行和列的数量, 均分有限的容器空间

Canvas : 使用固定的坐标设置元素的位置、不具备锚定停靠等功能。

Grid

学过web的应该知道table表格, 而Grid与其类似, Grid具备分割空间的能力。
RowDefinitions / ColumnDefinitions 用于给Grid分配行与列。
ColumnSpan / RowSpan 则用于设置空间元素的 跨列与阔行。

<Window x:Class="WpfApp1.MainWindow"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:WpfApp1"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><!--默认都是0,0开始--><Border Background="red"/><Border Grid.Row="1" Background="Yellow"/><Border Grid.Column="1" Background="Blue"/><Border Grid.Row="1" Grid.Column="1" Background="Green"/></Grid>
</Window>

此时页面被平均分为四个
在这里插入图片描述

自适应

若设置为自适应

<Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition/></Grid.RowDefinitions>

因为第一行没有任何元素和高度,整个元素 就被隐藏了
在这里插入图片描述

添加一个按钮定义宽高
以行内,最高元素高度为标准,来定义高度

<Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Button Width="100" Height="100"/><!--默认都是00开始--><Border Background="red"/><Border Grid.Row="1" Background="Yellow"/><Border Grid.Column="1" Background="Blue"/><Border Grid.Row="1" Grid.Column="1" Background="Green"/>
</Grid>

在这里插入图片描述

绝对尺寸
<Grid.RowDefinitions><RowDefinition Height="100"/><RowDefinition/>
</Grid.RowDefinitions>
比例

2* 代表第一行高度是第二行得两倍

<Grid.RowDefinitions><RowDefinition Height="2*"/><RowDefinition/>
</Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/>
</Grid.ColumnDefinitions>

在这里插入图片描述

元素跨行跨列

默认情况下,元素占一行一列
在这里插入图片描述

让他占据两列得空间

<Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Border Background="red" Grid.ColumnSpan="2"/>
</Grid>

在这里插入图片描述

两行

<Border Background="red" Grid.ColumnSpan="2"Grid.RowSpan="2"/>

在这里插入图片描述

StackPanel

StackPanel主要用于垂直或水平排列元素、在容器的可用尺寸内放置有限个元素,
元素的尺寸总和(长/高)不允许超过StackPanel的尺寸, 否则超出的部分不可见。

默认的排列方式,是从上往下

<Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><StackPanel><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/></StackPanel>
</Grid>

在这里插入图片描述
可以通过Orientation(Horizontal/Vertical) 设置排列方向

<Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><StackPanel Orientation="Horizontal"><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/></StackPanel>
</Grid>

在这里插入图片描述

WrapPanel

WrapPanel默认排列方向与StackPanel相反、WrapPanel的Orientation默认为Horizontal。
WrapPanel具备StackPanel的功能基础上具备在尺寸变更后自动适应容器的宽高进行换行换列处理。

<WrapPanel Grid.Row="1"><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/>
</WrapPanel>

在这里插入图片描述
也可以通过设置Orientation(Horizontal/Vertical) 设置排列方向

DockPanel

默认DockPanel中的元素具备DockPanel.Dock属性,
该属性为枚举具备: Top、Left、Right、Bottom。

默认情况下, DockPanel中的元素不添加DockPanel.Dock属性, 则系统则会默认添加 Left。

DockPanel有一个LastChildFill属性, 该属性默认为true, 该属性作用为, 当容器中的最后一个元素时, 默认该元素填充DockPanel所有空间。

<Window x:Class="WpfApp1.MainWindow"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:WpfApp1"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><DockPanel><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/></DockPanel></Grid>
</Window>

在这里插入图片描述
最后一个元素,跟前面的分开得原因是,最后一个元素默认填充剩余空间,但是他的宽度不够,就放在中间

将LastChildFill 设为False,最后一个元素就会跟随默认向左停靠

<Grid><DockPanel LastChildFill="False"><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/><Button Width="100" Height="40"/></DockPanel>
</Grid>

在这里插入图片描述
可以单独定义停靠方向

<Grid><DockPanel LastChildFill="False"><Button Width="100" Height="40" DockPanel.Dock="Left"/><Button Width="100" Height="40" DockPanel.Dock="Top"/><Button Width="100" Height="40" DockPanel.Dock="Right"/><Button Width="100" Height="40" DockPanel.Dock="Bottom"/></DockPanel>
</Grid>

在这里插入图片描述

UniformGrid

: 指定行和列的数量, 均分有限的容器空间

<Window x:Class="WpfApp1.MainWindow"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:WpfApp1"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><UniformGrid Columns="3" Rows="3"><Button/><Button/><Button/><Button/><Button/><Button/><Button/><Button/><Button/></UniformGrid></Grid>
</Window>

在这里插入图片描述
也可以不指定 行列数量

<Grid><UniformGrid><Button/><Button/><Button/><Button/><Button/><Button/><Button/><Button/><Button/></UniformGrid>
</Grid>

结果相同
在这里插入图片描述

项目作业

在这里插入图片描述

这篇关于WPF2022终结版系列课程笔记 1 WPF 基本布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/926608

相关文章

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

使用Python进行文件读写操作的基本方法

《使用Python进行文件读写操作的基本方法》今天的内容来介绍Python中进行文件读写操作的方法,这在学习Python时是必不可少的技术点,希望可以帮助到正在学习python的小伙伴,以下是Pyth... 目录一、文件读取:二、文件写入:三、文件追加:四、文件读写的二进制模式:五、使用 json 模块读写

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou