Grid——网格布局,其中控件或容器需指定位置

StackPanel——堆叠面板,其中的控件水平布局、竖直布局

DockPanel——停靠面板,内部控件或容器可以放置在上、下、左、右

WrapPanel——可以看作是具有自动换行功能的StackPanel容器。窗体太小时,其末尾的控件会自动换行。像Java中的流布局

布局原则:先整体规划(Grid),再局部规划(Grid、StackPanel等)

如下图,Grid有5行3列,具体布局、控件查看文档大纲

 

xaml代码

<Window x:Class="DemoWPF61.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:DemoWPF61"
mc:Ignorable="d"
Title="MainWindow" Height="240" Width="350">
<Grid >
<!--行定义,5行-->
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="50"/>
<RowDefinition Height="30"/>
<!--剩余高度-->
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<!--列定义,3列-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<!--网格的2,3两列放置StackPanel-->
<Grid Grid.Column="1" Grid.ColumnSpan="2">
<!--水平布局,右对齐-->
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<!--方式1-->
<Button Content="夕西行" Margin="5,0,0,0"/>
<!--方式2-->
<Button Margin="5,0,5,0">我的博客</Button>
</StackPanel>
</Grid>
<!--网格的1列2行放置Image,默认居中对齐-->
<Grid Grid.Column="0" Grid.Row="1">
<Image Source="C:/Users/Jv/Desktop/lena512.tiff"/>
</Grid>
<!--网格的1~3列放置StackPanel-->
<Grid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="2">
<!--水平布局,默认左对齐-->
<StackPanel Orientation="Horizontal">
<Button Margin="5,0,0,0">园子</Button>
<Button Margin="5,0,0,0">新闻</Button>
<Button Margin="5,0,0,0">博问</Button>
</StackPanel>
</Grid>
<Grid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="3">
<!--左右居中,上下居中-->
<Label Content="第4行,占三列" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<Grid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="4">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Margin="5,0,0,0">关于我们</Button>
<Button Margin="5,0,0,0">联系我们</Button>
<Button Margin="5,0,0,0">广告服务</Button>
<Button Margin="5,0,0,0">人才服务</Button>
<Button Margin="5,0,0,0">版权</Button>
</StackPanel>
</Grid>
</Grid>
</Window>

【GroupBox】GroupBox内只能有一个元素,可用StackPanel承载多个控件

布局、控件如图所示

  

<Window x:Class="DemoWPF61.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:DemoWPF61"
mc:Ignorable="d"
Title="MainWindow" Height="190" Width="200">
<!--StackPanel纵向布局,纵向对齐方式:底对齐-->
<StackPanel Orientation="Vertical" VerticalAlignment="Bottom" >
<!--GroupBox内只能有一个元素,StackPanel来承载更多的控件-->
<GroupBox Header="网站分类">
<!--StackPanel内,纵向布局-->
<StackPanel Orientation="Vertical">
<Button Content=".NET技术(16)"/>
<Button Content="编程语言(13)"/>
</StackPanel>
</GroupBox>
<GroupBox Header="链接">
<StackPanel Orientation="Vertical">
<Button Content="反馈和建议"/>
<Button Content="官方博客"/>
</StackPanel>
</GroupBox>
</StackPanel>
</Window>

【DockPanel】

  

<Window x:Class="DemoWPF61.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:DemoWPF61"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="230">
<DockPanel>
<StackPanel DockPanel.Dock="Top" >
<Label Content="StackPanel停靠在DockPanel的上边"/>
</StackPanel>
<StackPanel DockPanel.Dock="Bottom" Height="20">
</StackPanel>
<StackPanel DockPanel.Dock="Left" Width="30">
</StackPanel>
<StackPanel DockPanel.Dock="Right" Width="30"/>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content=" 中间地带,是我的天下" />
</StackPanel>
</DockPanel>
</Window>

【WrapPanel】可以看作是具有自动换行功能的StackPanel容器。默认从左到右排列。

   

左图最后一个Button的高度会变成最小尺寸。Orientation="Vertical"得到右图(默认水平布局)

<Window x:Class="DemoWPF61.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:DemoWPF61"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="209.199">
<WrapPanel Margin="3">
<Button Content="Top" VerticalAlignment="Top"/>
<Button Content="Bottom" VerticalAlignment="Bottom"/>
<Button Content="靠我撑大" MinHeight="40"/>
<Button Content="Center" VerticalAlignment="Center"/>
<Button Content="无对齐方式"/>
</WrapPanel>
</Window>

1、布局容器Grid、StackPanel、GroupBox、DockPanel、WrapPanel的更多相关文章

  1. C# - VS2019页面布局容器splitContainer和groupBox小结

    前言 在WinFrm应用程序中,产品的外观.布局将直接影响用户第一体验,所以对于开发者来说,在没有美工支持的前提下,应当注意系统页面的布局,本章主要讲解splitContainer和groupBox的 ...

  2. WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel

    回顾 上一篇,我们介绍了基本控件及控件的重要属性和用法,我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景,当 然这些都是本人在实际项目中的使用经验,可能还存在错误之处,还请大家指出. 本 ...

  3. ( 转)WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel

    回顾 上一篇,我们介绍了基本控件及控件的重要属性和用法,我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景,当 然这些都是本人在实际项目中的使用经验,可能还存在错误之处,还请大家指出. 本 ...

  4. 零元学Expression Blend 4 - Chapter 8 用实例了解布局容器系列-「Grid」

    原文:零元学Expression Blend 4 - Chapter 8 用实例了解布局容器系列-「Grid」 本系列将教大家以实做案例认识Blend 4 的布局容器,此章介绍的是Blend 4 里的 ...

  5. 零元学Expression Blend 4 - Chapter 10 用实例了解布局容器系列-「StackPanel」

    原文:零元学Expression Blend 4 - Chapter 10 用实例了解布局容器系列-「StackPanel」 本系列将教大家以实做案例认识Blend 4 的布局容器,此章介绍的布局容器 ...

  6. 重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid

    原文:重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGr ...

  7. 学习WPF——WPF布局——初识布局容器

    StackPanel堆叠布局 StackPanel是简单布局方式之一,可以很方便的进行纵向布局和横向布局 StackPanel默认是纵向布局的 <Window x:Class="Wpf ...

  8. WPF 10天修炼 第四天- WPF布局容器

    WPF布局 WPF的窗口也就是Window类,是一个内容控件,该控件派生自ContentControl.内容控件有一个Content属性,该属性有一个限制,只能放置一个用户界面元素,或一个字符串.为了 ...

  9. WPF布局容器

    1.StackPanel:堆栈面板,通过Orientation属性设置子元素的布局排列方向为“Vertical”(垂直)和“Horizontal”(水平),不写其默认值为“Vertical”,当设置为 ...

随机推荐

  1. AcWing登山

    这是2006北大举办的ACM的一道题. 题意为:给定景点海拔高度,队员们不去游览相同高度的景点,一开始往上爬,一但往下爬就不能再向上爬,求最多可以游览多少个景点.那么我们可以得到一个结论:以一个最高点 ...

  2. redis存储数据

    redis存储结构--5种 RedisTemplate访问Redis数据结构(一)--String https://blog.csdn.net/qq_25135655/article/details/ ...

  3. The Party and Sweets CodeForces - 1159C (拓排)

    优化连边然后拓排. #include <iostream> #include <sstream> #include <algorithm> #include < ...

  4. deepin 15.10.1 GTX1060 NVIDIA 驱动安装,双屏显示问题记录

    有一段时间没有用Linux了.由于买了个4k的戴尔显示屏,在deepin系统上无法用,从昨晚到现在,总于解决了我的问题! 问题1:无法直接在深度的显卡驱动管理器哪里直接切换,网上看到很多人都有这个问题 ...

  5. 设置Cookies

    设置Cookies: public ActionResult Index() { // if (Request.Cookies["user"] != null) { //Serve ...

  6. vue iview面包屑

    简单看一下vue,iview的面包屑怎么写呢? 简单的思路:1.获取到路由记录$route.matched 2.渲染 效果: 一.$route.matched 官网地址:https://router. ...

  7. access注入

    前面有自己总结详细的mysql注入,自己access注入碰到的比较少,虽然比较简单,但是这里做一个总结 union联合查询法: 因为union前后字段数相同,所以可以先用order by 22 使查询 ...

  8. mock.js 模拟数据

    1. 劫持请求,返回模拟数据: 用于前后台对接前数据模拟 相比于静态json文件而言:代码完成后不必修改源文件对应的接口调用.可模拟增删改查 2.实例代码 <!doctype html> ...

  9. hadoop的价值在哪里

    新霸哥注意到了在移动互联网时代,很多的传统的系统架构已经不能满足现有的业务需要了,在大数据时代,我们需要新的架构才能满足海量数据处理能力. 在实际的应用中数据增长非常的迅速,数据类型也变的丰富了,业务 ...

  10. python之SSH远程登录

    一.SSH简介 SSH(Secure Shell)属于在传输层上运行的用户层协议,相对于Telnet来说具有更高的安全性. 二.SSH远程连接 SSH远程连接有两种方式,一种是通过用户名和密码直接登录 ...