当前位置: 首页 > ds >正文

wpf之Grid控件

前言

Grid是wpf中的容器控件,可将窗体分割为多行多列,每个单元格可以放置控件,在wpf中得到非常频繁的应用。

1、1行1列

默认填充满整个窗体
在这里插入图片描述

<Window x:Class="wpf之Grid.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:wpf之Grid"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800" Background="Green" ><Grid Background="Blue"   Margin=" 10" ></Grid >
</Window>

2、1行多列

在这里插入图片描述

2.1 列宽度的设置

通过Grid.ColumnDefinitions来定义Grid有多少列,每一个ColumnDefinition都代表了一列,通过Width设置列的宽度。Width=“20” 代表使用绝对值宽度20,Width="2*"代表使用剩下宽度的比例,具体计算方式为把前的数字加起来作为分母,比如本文中就是2+1+1+1=5(*前面没写数字就是代表1*,不设置宽度也代表1*),所在列前面的数字作为分子,比如本文的第2列就是2/5,最终再乘以剩余没有被使用的宽度,相当于第2列占了剩余宽度的2份,3、4、5列各占了1份。

<Window x:Class="wpf之Grid.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:wpf之Grid"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800" Background="Green" ><Grid Background="Blue"   Margin=" 10" ><Grid.ColumnDefinitions><ColumnDefinition Width="20" /><ColumnDefinition Width="2*"/><ColumnDefinition Width="1*"/><ColumnDefinition Width="*"/><ColumnDefinition /></Grid.ColumnDefinitions><TextBlock Grid.Column=" 0" Text="列1" Foreground="Red" Background="Yellow"/><TextBlock Grid.Column=" 1" Text="列2" Foreground="Red"  Background="White"/><TextBlock Grid.Column=" 1" Text="列2" Foreground="Red"  Background="White"/><TextBlock Grid.Column=" 2" Text="列3" Foreground="Red" Background="Yellow"/><TextBlock Grid.Column=" 3" Text="列4" Foreground="Red" Background="White" /><TextBlock Grid.Column=" 4" Text="列5" Foreground="Red" Background="Yellow"/></Grid >
</Window>

2.2 在每个列中添加控件

通过Grid.Column=" 0" 来指定控件占据哪一列,0代表占用第一列(索引从0开始)。

3、1列多行

在这里插入图片描述

3.1 行高度的设置

通过Grid.RowDefinitions来定义Grid有多少行,每一个RowDefinition都代表了一行,通过Height设置列的高度。Height=“20” 代表使用绝对值高度20,Height="2*"代表使用剩下高度的比例,具体计算方式为把前的数字加起来作为分母,比如本文中就是2+1+1+1=5(*前面没写数字就是代表1*,不设置宽度也代表1*),所在列前面的数字作为分子,比如本文的第2列就是2/5,最终再乘以剩余没有被使用的高度,相当于第2行占了剩余高度的2份,3、4、5行各占了1份。

<Window x:Class="wpf之Grid.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:wpf之Grid"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800" Background="Green" ><Grid Background="Blue"   Margin=" 10" ><Grid.RowDefinitions  ><RowDefinition  Height ="20" /><RowDefinition Height="2*"/><RowDefinition Height="1*"/><RowDefinition Height="*"/><RowDefinition /></Grid.RowDefinitions><TextBlock Grid.Row =" 0" Text="行1" Foreground="Red" Background="Yellow"/><TextBlock Grid.Row=" 1" Text="行2" Foreground="Red"  Background="White"/><TextBlock Grid.Row=" 1" Text="行2" Foreground="Red"  Background="White"/><TextBlock Grid.Row=" 2" Text="行3" Foreground="Red" Background="Yellow"/><TextBlock Grid.Row=" 3" Text="行4" Foreground="Red" Background="White" /><TextBlock Grid.Row=" 4" Text="行5" Foreground="Red" Background="Yellow"/></Grid >
</Window>

3.2 在每个行中添加控件

通过Grid.Row==" 0" 来指定控件占据哪一行,0代表占用第一行(索引从0开始)。

4、多行多列

在这里插入图片描述
下面的代码中同时通过Grid.RowDefinitions 设置多行、Grid.ColumnDefinitions 设置多列,然后再通过给子控件TextBlock的 Grid.Row选择控件应该放置在哪一行,Grid.Column选择控件应该放置在哪一列。比如行2列2它的Grid.Row和Grid.Column都是1(索引从0开始)。

<Window x:Class="wpf之Grid.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:wpf之Grid"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800" Background="Green" ><Grid Background="Blue"   Margin=" 10" ><Grid.RowDefinitions  ><RowDefinition  Height ="20" /><RowDefinition Height="1*"/></Grid.RowDefinitions><Grid.ColumnDefinitions   ><ColumnDefinition Width  ="200" /><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><TextBlock Grid.Row =" 0" Grid.Column="0"  Text="行1列1" Foreground="Red" Background="Yellow"/><TextBlock Grid.Row=" 1" Grid.Column="0"  Text="行2列1" Foreground="Red"  Background="White"/><TextBlock Grid.Row=" 0" Grid.Column="1"  Text="行1列2" Foreground="Red"  Background="White"/><TextBlock Grid.Row=" 1" Grid.Column="1"  Text="行2列2" Foreground="Red" Background="Yellow"/></Grid >
</Window>

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》

http://www.xdnf.cn/news/18805.html

相关文章:

  • 鸿蒙分布式计算实战:用 ArkTS+Worker 池落地可运行任务管理 Demo,从单设备到跨设备全方案
  • 07-分布式能力与多设备协同
  • JDBC入门
  • DAY 55 序列预测任务介绍
  • 小红书自动评论插件
  • JUC之并发容器
  • 深度学习与自动驾驶中的一些技术
  • Java基础(十四)分布式
  • KingBase数据库迁移利器:KDTS工具深度解析与实战指南
  • golang6 条件循环
  • 01-鸿蒙系统概览与发展历程
  • Android面试指南(五)
  • 青少年机器人技术(二级)等级考试试卷-实操题(2024年9月)
  • C语言文件操作精讲:从格式化读写到随机访问
  • GOLANG 接口
  • Axure:如何打开自定义操作界面
  • loj数列分块入门2-3
  • c++string
  • crypto.randomUUID is not a function
  • 拓扑排序|hash
  • frp+go-mmproxy 实现透明代理的内网穿透
  • Qt5 高级功能
  • 关于说明锂电池充电芯片实际应用
  • 曲面方程的三维可视化:从数学解析到Python实现
  • 从罗永浩访谈李想中学习现代家庭教育智慧
  • 定时器互补PWM输出和死区
  • 54.Redis持久化-AOF
  • JEI(Journal of Electronic lmaging)SCI四区期刊
  • 控制建模matlab练习16:线性状态反馈控制器-⑤轨迹追踪
  • Linux内核进程管理子系统有什么第三十三回 —— 进程主结构详解(29)