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:/ ...
随机推荐
- Singleton bean creation not allowed while singletons of this factory are in destruction
1.背景 一直都是正常运行的程序,检查日志发现有一条报错如下: org.springframework.beans.factory.BeanCreationNotAllowedException: E ...
- 拈花云科基于 Apache DolphinScheduler 在文旅业态下的实践
作者|云科NearFar X Lab团队 左益.周志银.洪守伟.陈超.武超 一.导读 无锡拈花云科技服务有限公司(以下简称:拈花云科)是由拈花湾文旅和北京滴普科技共同孵化的文旅目的地数智化服务商.20 ...
- 【全】CSS动画大全之404页面【a】
效果预览 代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...
- MySQL编译安装-麒麟V10 x86
环境信息 操作系统: Kylin Linux Advanced Server V10 (Sword) 架构:X86 MySQL版本:5.7.44 编译 安装必要的依赖库和编译工具 sudo yum g ...
- 虚拟文件系统VFS-片段一
文件系统类型 基于磁盘的文件系统 如FAT.EXT4 虚拟文件系统 如proc 网络文件系统 顾名思义,网络文件系统还将网络通信封装起来,这意味可以直接通过通信访问另一台设备的文件系统. man fs ...
- ai识图测试
var code = "9392b629-0d84-43ef-9b0f-34740fb024a6"
- Go channel 介绍
Go 语言(Golang)中的 chan 是通道(channel)的缩写,用于在不同的 goroutine 之间进行通信.通道允许你在 goroutine 之间传递数据,从而实现同步和共享内存.下面是 ...
- Android Adapter分页显示异常: 首页数据在屏幕内全显示,无法上滑分页
前情提要: Android 端Adapter分页,下拉刷新,上滑分页, 原有流程:上滑时,滑动触及底部,触发Adapter的onScrolled的方法 异常情况:第一页数据完全显示,导致上滑时,未触及 ...
- JDK有用的新特性-Switch
目录 箭头表达式,新的 case 标签 yeild 返回值 Java Record Switch 的三个方面,参考: JEP 361 支持箭头表达式 支持 yied 返回值 支持 Java Recor ...
- JavaScript 中 structuredClone 和 JSON.parse(JSON.stringify()) 克隆对象的区别
JavaScript 中 structuredClone 和 JSON.parse(JSON.stringify()) 克隆对象的异同点 一.什么是 structuredClone? 1. struc ...