WPF学习-布局
1. Grid布局 ,(Table 布局)
两行两列布局,
Border 0 行 0 列默认开始
<Window x:Class="WpfApp.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:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="Red"></Border>
<Border Grid.Row="1" Background="Yellow"></Border>
<Border Grid.Column="1" Background="Blue"></Border>
<Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
</Grid>
</Window>
效果图:

2. StackPanel 布局
默认垂直布局 ,一旦超出区域限制后不限制
<StackPanel Orientation="Horizontal"> 改成水平排列
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<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>
效果图:

3. WrapPanel 布局, ( float布局)
默认水平排序
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<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>
<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>
</Grid>
效果:

4. DockPanel 停靠 (flex 布局)
默认横向填充,
<Grid>
<DockPanel>
<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"/>
</DockPanel>
</Grid>
效果图:
默认横向填充, 最后一个元素占据整个布局, 居中显示.

停靠布局
注意设置: LastChildFill="False"
<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>
效果图:

5. Uniform 布局 (Table)
均分所有区域
设置三行三列布局
<UniformGrid Columns="3" Rows="3">
<Button></Button>
<Button></Button>
<Button></Button>
<Button></Button>
<Button></Button>
<Button></Button>
<Button></Button>
<Button></Button>
</UniformGrid>
效果图:

6. 布局Demo 案例
Border : 类似background 属性
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<!--装饰器元素-->
<Border Background="#7671d8"></Border>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Background="Blue"></Border>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Border Margin="5" Background="#7671d8"></Border>
<Border Margin="5" Background="#429ecd" Grid.Column="1"></Border>
<Border Margin="5" Background="#7671d8" Grid.Column="2"></Border>
<Border Margin="5" Background="#5ac4b6" Grid.Column="3"></Border>
<Border Margin="5" Background="#d9707f" Grid.Column="4"></Border>
<Border Background="Red" Grid.Row="1" Grid.ColumnSpan="3" Margin="5">
</Border>
<Border Background="Yellow" Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="2" Margin="5">
</Border>
<Border Grid.Row="2" Background="Blue" Margin="5" Grid.ColumnSpan="3"></Border>
<Border Grid.Row="2" Grid.ColumnSpan="2" Grid.Column="3" Background="Green" Margin="5"></Border>
</Grid>
</Grid>
</Grid>

7. 定义样式
<Window.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background" Value="Red"></Setter>
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Content" Value="Button1"></Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel >
<Button Style="{StaticResource ButtonStyle}" ></Button>
</StackPanel>
</Grid>
样式继承,使用 BasedOn 实现继承
<Window.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Background" Value="Red"></Setter>
<Setter Property="FontSize" Value="18"></Setter>
</Style>
<Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource ButtonStyle}">
<Setter Property="Content" Value="Button1"></Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel >
<Button Style="{StaticResource ButtonStyle}" ></Button>
</StackPanel>
</Grid>
控件模板控件模板控件模板控件模板控件模板控
WPF学习-布局的更多相关文章
- 迟到的 WPF 学习 —— 布局
布局是 WPF 很重头的一部分内容,这一部分梳理和记录关于布局章节的知识点. 1. WPF 使用一种基于流(Flow-based)的概念来处理布局逻辑,将传统的基于"坐标"的思想尽 ...
- WPF编程学习——布局
本文目录 1.布局简介 2.面板(Panel) 3.视图框(Viewbox) 4.滚动视图控件(ScrollViewer) 5.公共布局属性 1.布局简介 应用程序界面设计中,合理的元素布局至关重要, ...
- WPF学习(3)布局
今天我们来说说WPF的布局.我们知道WinForm的布局主要是采用基于坐标的方式,当窗口内容发生变化时,里面的控件不会随之动态调整,这就造成了一个很不好的用户体验.而WPF为了避免这个缺点,采用了基于 ...
- WPF学习开发客户端软件-任务助手(下 2015年2月4日代码更新)
时光如梭,距离第一次写的 WPF学习开发客户端软件-任务助手(已上传源码) 已有三个多月,期间我断断续续地对该项目做了优化.完善等等工作,现在重新向大家介绍一下,希望各位可以使用,本软件以实用性为主 ...
- WPF学习05:2D绘图 使用Transform进行控件变形
在WPF学习04:2D绘图 使用Shape绘基本图形中,我们了解了如何绘制基本的图形. 这一次,我们进一步,研究如何将图形变形. 例子 一个三角形,经Transform形成组合图形: XAML代码: ...
- WPF学习之路初识
WPF学习之路初识 WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...
- WPF学习拾遗(二)TextBlock换行
原文:WPF学习拾遗(二)TextBlock换行 下午在帮组里的同事解决一个小问题,为了以后方便,把就把它收集一下吧. 新建一个TextBlock作为最基础的一个控件,他所携带的功能相对于其他的控件要 ...
- WPF(布局)
WPF编程学习——布局 本文目录 1.布局简介 2.面板(Panel) 3.视图框(Viewbox) 4.滚动视图控件(ScrollViewer) 5.公共布局属性 1.布局简介 应用程序界面 ...
- Wpf之布局
Wpf之布局 上一章大家有了自己的一个Hello World的wpf程序,今天咱们就一起走进WPF,一起来看看wpF的前台xaml这门语言的魅力. 写过web 的人都知道布局这个概念,在web中布局和 ...
- WPF学习:3.Border & Brush
上一章<WPF学习:2.Layout-Panels-Countainers>主要介绍了布局,容器和面板.这一章主要开始介绍Border(边界)和Brush(画刷). 代码地址:http:/ ...
随机推荐
- 社区6月月报 | Apache DolphinScheduler重要修复和优化记录
各位热爱Apache DolphinScheduler的小伙伴们,社区6月月报更新啦!这里将记录Apache DolphinScheduler社区每月的重要更新,欢迎关注. 月度Merge Stars ...
- Java数组小白版
一.数组概念 一.数组定义 数组就是指在计算机内存中开辟的连续存储空间,用于存放程序运行中需要用到的一组相同类型数据的容器. 二.数组的声明 +数组的长度 定义数组时需要确定数组的长度(元素的个数), ...
- Java 大文件IO操作效率对比【我说说 你瞅瞅】
Java 文件IO操作效率对比 注:本文只做时间消耗层面对比,内存占用层面需要特别关注! 1. 参数说明 文件总大小:2,111,993,850 字节(2.11 GB) static String d ...
- git使用问题记录
hint: Updates were rejected because the remote contains work that you do 问题原因: 远程仓库中含有本地仓库没有的文件 直接拉取 ...
- uni-app 路由封装(简易版)
在实践运用中,经常需要在路由跳转时判断是否需要登录权限,页面跳转时,添加加在if判断. 插件市场也有一些这种插件,配置也稍微复制,大部分朝向vue-router. 注:本次路由封装,只是单纯的判断是否 ...
- 淘宝打单发货接口,淘宝打单发货API
许多做系统功能的小伙伴经常面对的一个功能是对接淘宝开放平台,在自己系统中进行打单发货. 但是,目前淘宝开放平台,已经关闭了相关的相关的权限申请,具体可查看相关公告.有需要这个权限的,可以站内信联系我, ...
- springboot 集成 onlyoffice 实现文档预览、编辑、pdf转化、缩略图生成
开源地址 https://gitee.com/lboot/lucy-onlyoffice 介绍 lucy-onlyoffice是依赖于onlyoffice的springboot文档预览编辑集成解决方案 ...
- 小tips:docker 配置国内镜像地址
在配置文件daemon.json中添加国内镜像,让其下载加速. vi /etc/docker/daemon.json 如下国内镜像: { "registry-mirrors": [ ...
- 【笔记】利用七牛云CDN加速提高前端图片静态资源访问速率
第一步,注册七牛云账号 打开 七牛开发者平台,选择 注册,注册后登录系统然后实名认证. 第二步,创建七牛云空间 七牛对象存储将数据文件以资源的形式上传到空间中.您可以创建一个或者多个空间,然后向每个空 ...
- GPUStack正式发布: 为大模型而生的开源GPU集群管理器
经过 Seal 研发团队几个月来持续的开发和测试,我们满怀期待及热情地发布新的产品 GPUStack,GPUStack 是一个用于运行 LLM(大型语言模型)的开源 GPU 集群管理器.尽管如今大语言 ...