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>

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

项目作业

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/640602.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STP学习的第一篇

1.STP的基本概念&#xff1a;根桥 &#xff08;1&#xff09;STP的主要作用之一是在整个交换网络中计算出一棵无环的“树”&#xff08;STP树&#xff09;。 &#xff08;2&#xff09;根桥是一个STP交换网络中的“树根”。 &#xff08;3&#xff09;STP开始工作后&#xf…

K8s: Ingress对象, 创建Ingress控制器, 创建Ingress资源并暴露服务

Ingress对象 1 &#xff09;概述 Ingress 是对集群中服务的外部访问进行管理的 API 对象&#xff0c;典型的访问方式是 HTTPIngress-nginx 本质是网关&#xff0c;当你请求 abc.com/service/a, Ingress 就把对应的地址转发给你&#xff0c;底层运行了一个 nginx但 K8s 为什么不…

OpenCV-基于阴影勾勒的图纸清晰度增强算法

作者&#xff1a;翟天保Steven 版权声明&#xff1a;著作权归作者所有&#xff0c;商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处 实现原理 大家在工作和学习中&#xff0c;无论是写报告还是论文&#xff0c;经常有截图的需求&#xff0c;比如图表、图纸等&…

无人驾驶 自动驾驶汽车 环境感知 精准定位 决策与规划 控制与执行 高精地图与车联网V2X 深度神经网络学习 深度强化学习 Apollo

无人驾驶 百度apollo课程 1-5 百度apollo课程 6-8 七月在线 无人驾驶系列知识入门到提高 当今,自动驾驶技术已经成为整个汽车产业的最新发展方向。应用自动驾驶技术可以全面提升汽车驾驶的安全性、舒适性,满足更高层次的市场需求等。自动驾驶技术得益于人工智能技术的应用…

SpringBoot 根据不同环境切换不同文件路径

最简单的办法就是使用多个 application.yml 配置文件 。一个叫 application-test.yml 测试用&#xff1b;另一个是正式使用的 application-prod.yml 。win环境下大部分是开发测试时候使用的&#xff0c;服务正式上线需要部署在Linux服务器上又换成了Linux。但开发初期或者项目…

JS-47-Node.js06-fs模块-读写文件

Node.js内置的fs模块就是文件系统模块&#xff0c;负责读写文件。 和所有其它JavaScript模块不同的是&#xff0c;fs模块同时提供了异步和同步的方法。 一、回顾&#xff1a;异步方法VS同步方法 1-1、异步方法 因为JavaScript的单线程模型&#xff0c;执行IO操作时&#xff…

Linux及tmux、vim常用命令

Linux 关于Linux的简介、诞生、迭代&#xff0c;大家可以去网上查一查&#xff0c;这里不多做赘述了 Linux文件类型 非常重要的文件类型有: 普通文件&#xff0c;目录文件&#xff0c;链接文件&#xff0c;设备文件&#xff0c;管道文件&#xff0c;Socket 套接字文件 等。 …

【Leetcode每日一题】 分治 - 数组中的第K个最大元素(难度⭐⭐)(63)

1. 题目解析 题目链接&#xff1a;数组中的第K个最大元素 这个问题的理解其实相当简单&#xff0c;只需看一下示例&#xff0c;基本就能明白其含义了。 2.算法原理 在快速排序算法中&#xff0c;一种常见的优化策略是将数组划分为三个区间。这种划分方式可以更加精确地定位到…

我看谁还不会这几个 TypeScript 高级技巧

typescript 作为前端使用最多的框架之一&#xff0c;快来看看下面这些隐藏的高级技巧吧 Mapped Types 当我们在声明类型的时候&#xff0c;可以借助 Mapped Types 的方式对基础类进行扩展&#xff0c;这样就可以减少定义重复的基础类型 type Ev {add: string;remove: strin…

day_8题解

利用最大公约数求最小公倍数 #include<iostream> using namespace std;int gcd(int a,int b) {return b?gcd(b,a%b):a; }int main() {long long a,b;cin>>a>>b;long long ansgcd(a,b);cout<<(a*b)/ans<<endl;return 0; }排序遍历&#xff0c;记…

小游戏:贪吃蛇

&#x1f381;个人主页&#xff1a;我们的五年 &#x1f50d;系列专栏&#xff1a;贪吃蛇 &#x1f337;追光的人&#xff0c;终会万丈光芒 目录 &#x1f3dd;1.头文件&#xff1a; &#x1f3dd;2.实现文件&#xff1a; &#x1f3dd;3.测试文件 &#xff1a; 前言&#…

阿里云X魔搭社区Create@AI创客松第四届冠军:MumuLab

4月13日终于迎来了线下Demo Day&#xff0c;此前阿里云 X 魔搭社区 X Datawhale CreateAI创客松已经紧锣密鼓地准备了一个多月时间&#xff0c;全球150团队报名、创作出66作品、评选出25支团队进入决赛&#xff0c;作品范围覆盖从办公效率到法律调解再到游戏互动以及构建童话世…