C# WPF:初识布局容器

StackPanel堆叠布局

StackPanel是简单布局方式之一,可以很方便的进行纵向布局和横向布局 StackPanel默认是纵向布局的

复制代码

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<StackPanel> 
<Button Content="按钮"></Button>
<Button Content="按钮"></Button>
<Button Content="按钮"></Button>
<Button Content="按钮"></Button>
<Label Content="Label"></Label>
<Label Content="Label"></Label>
<Label Content="Label"></Label>
</StackPanel> 
</Window>

复制代码

如果要横向布局的话,只要把StackPanel的Orientation属性设置成Horizontal即可

这个属性的默认值是Vertical

复制代码

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<StackPanel Orientation="Horizontal"> 
<Button Content="按钮"></Button>
<Button Content="按钮"></Button>
<Button Content="按钮"></Button>
<Button Content="按钮"></Button>
<Label Content="Label"></Label>
<Label Content="Label"></Label>
<Label Content="Label"></Label>
</StackPanel> 
</Window>

复制代码

WrapPanel包裹布局
在WrapPanel面板中的元素以一次一行或一列的方式布局控件
WrapPanel也有Orientation属性,但与StackPanel不同的是,WrapPanel的Orientation属性的默认值是Horizontal
也就是说WrapPanel的默认展现方向是横向的
WrapPanel与StackPanel另一个不同的地方是,当容器实际宽度不够的情况下,内容将以多行或者多列的形式展现

复制代码

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<WrapPanel>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
</WrapPanel>
</Window>

复制代码

 WrapPanel的纵向展现方式

复制代码

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<WrapPanel Orientation="Vertical">
<Button Content="allen1"></Button>
<Button Content="allen2"></Button>
<Button Content="allen3"></Button>
<Button Content="allen4"></Button>
<Button Content="allen5"></Button>
<Button Content="allen6"></Button>
<Button Content="allen7"></Button>
<Button Content="allen8"></Button>
<Button Content="allen9"></Button>
<Button Content="allen10"></Button>
</WrapPanel>
</Window>

复制代码

DockPanel停靠布局
这种布局把布局容器分为上、下、左、右四个边缘,容器内的元素沿着某一个边缘来拉伸自己

复制代码

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<DockPanel>
<!--沿着上边缘拉伸-->
<Button Content="Top" DockPanel.Dock="Top"></Button>
<!--沿着下边缘拉伸-->
<Button Content="Bottom" DockPanel.Dock="Bottom"></Button>
<!--沿着左边缘拉伸-->
<Button Content="Left" DockPanel.Dock="Left"></Button>
<!--沿着右边缘拉伸-->
<Button Content="Right" DockPanel.Dock="Right"></Button>
<!--默认沿着左边缘拉伸-->
<Button Content="allen5"></Button>
<!--默认沿着左边缘拉伸-->
<Button Content="allen6"></Button>
<!--最后一个元素默认填充满整个容器剩余的空间-->
<Button Content="默认最后一个自适应"></Button>
</DockPanel>
</Window>

复制代码

Grid表格布局
Grid布局容器可以把空间分割成多行多列,用以摆放不同的控件

复制代码

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<Grid>
<!--定义两行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--定义三列-->
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--Grid.Row或 Grid.Column的默认值为0-->
<Button Content="默认在第一行第一列且填充"></Button>
<!--如果我把Grid.Row的值设置成2,因为没有第三行,所以按钮会自动被放在最后一行,仍然是第二行-->
<Button Grid.Row="1" Grid.Column="1" Content="第二行第二列"></Button>
</Grid>
</Window>

复制代码

Canvas画布布局
Canvas画布布局容器允许使用精确的坐标来摆放画布内的元素
如果两个元素共用了同一块区域,那么后设置的元素将覆盖先设置的元素

复制代码

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<Canvas>
<Button Canvas.Left="100" Canvas.Top="100" Content="第一个按钮"></Button>
<Button Canvas.Left="136" Canvas.Top="112" Content="第二个按钮"></Button>
</Canvas>
</Window>

复制代码

Window窗口
窗口是容纳所有WPF界面元素的最初容器,任何的界面元素都要放在Window窗口内才能呈现
WPF窗口只能包含一个儿子控件,这是因为Window类继承自ContentControl类。

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<!--你不能在这里放置多个同级元素-->
</Window>

ContentControl就是我们常说的内容控件,这种控件与容器控件(Grid或StackPanel)不同,
内容控件的顶级子元素只能有一个,容器控件可以包含多个顶级子元素
如果我们想要在一个ContentControl内展示多个子控件,
我们可以先放置一个容器控件作为内容控件的顶级子元素,然后再在此容器控件中放置更多的控件

复制代码

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow" Height="350" Width="525"> 
<Grid>
<Button Content="Button" />
<Button Content="Button" />
</Grid>
</Window>

复制代码

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

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

相关文章

Kibana源码分析--Hapijs路由设置理解笔记

【ES6解构赋值】&#xff1a;https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment 【Joi APi】&#xff1a;https://github.com/hapijs/joi/blob/v13.1.2/API.md 转载于:https://www.cnblogs.com/lishidefengchen/p/866874…

Python打包EXE神器 pyinstaller

最近由于项目需要&#xff0c;以前的python文件需要编辑为EXE供前端客户使用。 由于最早接触的是distutils&#xff0c;所以一开始准备使用distutils和py2exe搭配来进行python的exe化&#xff0c;也就是传统的使用setup.py的方式来进行exe安装。但是结果都不是很好&#xff0c;…

好程序员HTML5前端教程-css的引入方式和选择器

好程序员HTML5前端教程-css的引入方式和选择器 01.引入css方式&#xff08;重点掌握&#xff09; 行内样式 内接样式 外接样式      3.1 链接式      3.1 导入式 css介绍 现在的互联网前端分三层&#xff1a; HTML&#xff1a;超文本标记语言。从语义的角度描述页面结…

4.4.6 数组也能无锁:AtomicIntegerArray

数组也可以实现cas操作&#xff0c;有以下几个类以及用法如下&#xff1a; public class AtomicTntegerArrayTest {public static void main(String[] args) {AtomicIntegerArray atomicIntegerArraynew AtomicIntegerArray(3);AtomicLongArray atomicIntegerArray1new AtomicL…

20种PLC元件编号和Modbus编号地址对应表

1、三菱&#xff1a; X元件支持Modbus之02功能码&#xff1b; Y元件支持Modbus之01、05、15功能码&#xff1b; D元件支持Modbus之03、06、16功能码。 2、西门子&#xff1a; I元件支持Modbus之02功能码&#xff1b; Q元件支持Modbus之01、05、15功能码&#xff1b; V元件…

暑期学习

由于最后大作业的呈现情况与短学期所完成的还相差甚远&#xff0c;所以在暑期的时候开始进一步的细化。 在这个过程之中产生了如下的问题&#xff1a; 已解决的有&#xff1a; 1.用a标签在同一页面实现跳转。 要点&#xff1a;标记<a href"../home#pre">的时候…

五、RabbitMQ的消息属性(读书笔记)

2019独角兽企业重金招聘Python工程师标准>>> 简介 当使用RabbitMQ发布消息时&#xff0c;消息又AMQP规范中的三个低层帧类型组成&#xff1a; Basic.publish方法帧&#xff1b;内容头帧&#xff1b;消息体帧&#xff1b;这三种帧类型按顺序一起工作&#xff0c;以便…

异步和单线程

转载于:https://www.cnblogs.com/sunmarvell/p/8674748.html

windows下解决mysql5中文乱码的问题

1.问题描述&#xff1a;一开始无论是在命令行&#xff0c;还是在mysql的客户端输入中文都会出现 “???” 问题之类的乱码问题&#xff1b; 2.解决办法&#xff1a; 1&#xff09;cmd 进入mysql &#xff0c;命令mysql -uroot -p123456 2&#xff09;然后执行 show variable…

C#:把dll封入exe中方法

在这个事件中,可以重新为加载失败的程序集手动加载 如果你将dll作为资源文件打包的你的应用程序中(或者类库中) 就可以在硬盘加载失败的时候 从资源文件中加载对应的dll 就像这样: class Program {static Program(){ //这个绑定事件必须要在引用到TestLibrary1这个程序…

P2685 [TJOI2012]桥

P2685 [TJOI2012]桥 思路&#xff1a; 先求出最短路&#xff1a; d1[u] : u 到 1 的最短路&#xff0c; d2[u] : u 到 n 的最短路 再求出一条从 1 到 n 的最短路链&#xff0c;然后从链上的每一个点出发dfs, 求出&#xff1a; l[u] : u 到 1 的最短路径过中和链的交点&#xf…

C#结构类型图

转载于:https://www.cnblogs.com/kangao/p/8674838.html

C# 全局钩子实现扫码枪获取信息

1.扫描枪获取数据原理基本相当于键盘数据&#xff0c;获取扫描枪扫描出来的数据&#xff0c;一般分为两种实现方式。 a&#xff09;文本框输入获取焦点&#xff0c;扫描后自动显示在文本框内。 b&#xff09;使用键盘钩子&#xff0c;勾取扫描枪虚拟按键&#xff0c;根据按键频…

Centos下安装mysql(二进制版)

Centos下安装mysql&#xff08;二进制版&#xff09; 1.下载安装包&#xff0c;选择相应的平台、版本&#xff0c;比如&#xff0c;选择64位Linux平台下的MySQL二进制包“Linux-Generic &#xff08;glibc 2.5&#xff09;&#xff08;x86&#xff0c;64-bit&#xff09;&#…

使用gradle多渠道打包

以友盟的多渠道打包为例&#xff0c;如果我们须要打包出例如以下渠道&#xff1a;UMENG, WANDOUJIA, YINGYONGBAO。 第一种方法。是须要创建文件的。我们在写完我们的代码之后&#xff0c;在app/src以下。分别创建和main同级目录的目录umeng, wandoujia, yingyongbao,这三个目录…

SMMS 2016 啟用深色主題

1、用文本類編輯器 打開C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio目錄下的 ssms.pkgundef 2、去除// Remove Dark theme行以下的注釋 3、重新打開SMMS&#xff0c;如果還沒有出現“深色”主題&#xff0c;請執行第4點 4、打開powershell【…

四大步骤,彻底关闭Win10自动更新

尽管Win11已经发布了一段时间&#xff0c;但目前互联网上大部分电脑用户所使用的的操作系统仍是Win10&#xff0c;对于Win10&#xff0c;笔者相信大部分人应该都不陌生&#xff0c;作为目前市面上占比最高的电脑系统&#xff0c;Win10的许多功能和操作逻辑都十分优秀&#xff0…

LeetCode算法题-Repeated String Match(Java实现)

这是悦乐书的第289次更新&#xff0c;第307篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第156题&#xff08;顺位题号是686&#xff09;。给定两个字符串A和B&#xff0c;找到A必须重复的最小次数&#xff0c;使得B是它的子字符串。 如果没有这样的解决方案&a…

php

●转载于:https://www.cnblogs.com/volcanorao/p/8678104.html

Vs快捷键设置(可搭配Vim使用)

设置方式: 通过在Vs菜单栏的工具->选项->环境->键盘。 常用快捷键: 推荐键位编辑.转到定义Alt G切换标题代码文件Alt Q查看.向前导航Alt D查看.向后导航Alt A调试.调用堆栈Alt 7调试.监视1Alt 8调试.内存1Alt 9查看.查找符号结果Alt 1查看.错误列表Alt …