一.WPF布局原理

WPF窗口只能包含单个元素,为在WPF窗口中放置多个元素,需要放置一个容器,让后在容器中添加其他元素。“理想的”WPF窗口需遵循以下几个原则:

1.不应显示设定元素的尺寸。元素应当可以改变尺寸适合他们的内容,如添加更多文字内容时,按钮会适当扩展。设置最大,最小尺寸,控制尺寸范围。

2.不应使用确定坐标确定元素的位置。元素应当由容器根据他们的尺寸,按一定的顺序进行排列。Margin属性可添加空白空间。

3.布局容器的子元素“共享”可用空间。布局容器根据每个元素的内容尽可能的为元素设置合理尺寸。

4.可嵌套的布局容器。如Grid面板,可以嵌套多种元素,如文本框,按钮。

核心布局面板

名称 说明
StackPanel 水平或垂直的堆栈中放置元素。
WrapPanel 在一系列可换行的行中放置元素。
DockPanel 根据整个容器的边界调整元素。
Grid 多行多列中排放元素,适用面最广
Canvas 使用固定坐标定位元素,对于可变尺寸的窗口,不是合适选择。
   

二.个核心面板介绍

1.StackPanel面板

  <StackPanel>
<Label HorizontalAlignment="Center">A Button Stack </Label> //对齐方式
<Button HorizontalAlignment="Left">button1</Button>
<Button HorizontalAlignment="Right">button2</Button>
<Button Margin="5">button3</Button> //边距为5
<Button Margin="10,5,10,5">button4</Button> //分别设置边距
</StackPanel>

2.Border控件

Border不是布局面板但是可以和控制面板搭配使用。

     <Border Background="Yellow" Margin="5" Padding="5" BorderBrush="SteelBlue" CornerRadius="5" VerticalAlignment="Top">
<StackPanel Margin="3">
<Label HorizontalAlignment="Center">A Button Stack</Label>
<Button Margin="3" MaxWidth="200" MinWidth="100">button1</Button> //设置最大宽最小宽
<Button Margin="3" MaxWidth="200" MinWidth="100">button2</Button>
<Button Margin="3" MaxWidth="200" MinWidth="100">button3</Button>
</StackPanel>
</Border>

      

3.WrapPanel和DockPanel面板

     <WrapPanel Margin="3">
<Button VerticalAlignment="Top">Top button1</Button>
<Button MinHeight="60">Tall button2</Button>
<Button VerticalAlignment="Bottom">Bottom button3</Button>
<Button VerticalAlignment="Center">Center button4</Button>
</WrapPanel>

注意:WrapPanel是唯一一个无法通过灵活使用Grid面板代替的面板

4.DockPanel面板

DockPanel面板沿一条外边缘拉升所包含的控件

     <DockPanel Margin="3">
<Button DockPanel.Dock="Top">Top button1</Button>
<Button DockPanel.Dock="Top" HorizontalAlignment="Center"> button2</Button>
<Button DockPanel.Dock="Top" HorizontalAlignment="Left"> button3</Button>
<Button DockPanel.Dock="Bottom"> button4</Button>
<Button DockPanel.Dock="Left"> button5</Button>
<Button DockPanel.Dock="Right"> button6</Button>
<Button>bb</Button>
</DockPanel>

停靠行为保持不变,首先停靠顶部按钮,然后停靠底部按钮,剩余空间被分割,最后按钮在中间。

5.Grid面板及嵌套问题

Grid面板通常包含多个GridSplitter对象,可以在一个Grid面板中嵌套另一个Grid面板,如果在Grid面板中嵌套了Grid对象,那么每个单独的Grid都有自己的GridSplitter对象,这样可以创建被分割成两部分的窗口,还可以进行进一步的分割。

    <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition> </ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions> <Grid Grid.Column="0" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions> <Button Margin="3" Grid.Row="0">top left</Button>
<Button Margin="3" Grid.Row="1">botton left</Button> </Grid> <GridSplitter Grid.Column="1" Width="3" HorizontalAlignment="Center" VerticalAlignment="Stretch" ShowsPreview="False"></GridSplitter> <Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions> <Button Grid.Row="0" Margin="3">top right</Button>
<Button Grid.Row="2" Margin="3">bottom right</Button> <GridSplitter Grid.Row="1" Height="3" HorizontalAlignment="Stretch" VerticalAlignment="Center" ShowsPreview="False"></GridSplitter>
</Grid>
</Grid>

WPF 布局总结的更多相关文章

  1. 对比MFC资源文件谈谈WPF布局方式

    对比MFC资源文件谈谈WPF布局方式 MFC方式 对于传统的MFC基于UI的应用程序设计通常分两步走,首先是设计UI,使用的是RC文件,然后是代码文件,对RC文件进行操作,如下面Figure 1 的基 ...

  2. WPF快速入门系列(1)——WPF布局概览

    一.引言 关于WPF早在一年前就已经看过<深入浅出WPF>这本书,当时看完之后由于没有做笔记,以至于我现在又重新捡起来并记录下学习的过程,本系列将是一个WPF快速入门系列,主要介绍WPF中 ...

  3. 学习WPF——WPF布局——了解布局容器

    WPF布局工作内部原理 WPF渲染布局时主要执行了两个工作:测量和排列 测量阶段,容器遍历所有子元素,并询问子元素所期望的尺寸 排列阶段,容器在合适的位置放置子元素,并设置元素的最终尺寸 这是一个递归 ...

  4. 浅谈 WPF布局

    我们首先来了解一下图形化用户界面(Graphic User Interface)也就是我们常常听到的GUI.举个简单的例子,同样是数据,我们可以用控制台程序加格式控制符等输出,但是这些都不如GUI来的 ...

  5. WPF布局系统[转]

    转自:http://www.cnblogs.com/niyw/archive/2010/10/31/1863908.html前言 前段时间忙了一阵子Google Earth,这周又忙了一阵子架构师论文 ...

  6. 意外地解决了一个WPF布局问题

    原文:意外地解决了一个WPF布局问题 今天做了一个小测试,意外地将之前的一个困扰解决了,原问题见<WPF疑难杂症会诊>中的“怎么才能禁止内容撑大容器?” 以前我是在外侧嵌套Canvas容器 ...

  7. WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系

    WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系: 1.Canvas/WrapPanel控件: 其子控件的HorizontalAlign ...

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

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

  9. WPF布局控件常用属性介绍

    WPF布局控件常用属性介绍 其它 | 作者:慧都控件网 | 2011-04-06 13:41:57| 阅读 0次 有用(0) 评论(0)   概述:WPF布局控件都是派生自System.Windows ...

随机推荐

  1. [BZOJ 1295][SCOI2009]最长距离(SPFA+暴力)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1295 分析:很巧妙的一道spfa从搜索的角度是搜索在所有1中搜索删除哪T个1,对整个图询问,这 ...

  2. MVC——应用Ajax获取不到数据问题解答

    当我们使用控制器利用Ajax获取表单数据时,调试为null,这时看看你接受表单时定义的参数名字是否为action 其实不能起这个名字的,这个名字和控制器关键字冲突了 随便换个其它名字就好了,比如我起个 ...

  3. usefull-url

    http://www.johnlewis.com/ http://codepen.io/francoislesenne/pen/aIuko http://www.rand.org/site_info/ ...

  4. .net架构设计读书笔记--第一章 基础

    第一章 基础 第一节 软件架构与软件架构师  简单的说软件架构即是为客户构建一个软件系统.架构师随便软件架构应运而生,架构师是一个角色. 2000年9月ANSI和IEEE发布了<密集性软件架构建 ...

  5. 【HTTP劫持和DNS劫持】腾讯的实际业务分析

        简单介绍一下HTTP劫持和DNS劫持的概念,也就是运营商通过某些方式篡改了用户正常访问的网页,插入广告或者其他一些杂七杂八的东西.       首先对运营商的劫持行为做一些分析,他们的目的无非 ...

  6. 未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”

     未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” 使用nu ...

  7. 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...

  8. BZOJ-1925 地精部落 烧脑DP+滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

  9. JAVA输入一个整数,求出其所有质因数

    首先得求出能整除A的数,再判断I是否是质数!!! import java.util.*; public class aa { public static void main(String[] args ...

  10. Handler.dispatchMessage handleMessage

    "main@3972" prio=5 runnable java.lang.Thread.State: RUNNABLE at com.ease.financialoa.modul ...