WPF基础学习笔记整理 (四) 布局
- WPF使用的是容器(container)进行布局;
- WPF窗口(Window类型)只能包含单个元素,故为了放置多个元素并增强界面效果,引入了容器;
- WPF布局容器都派生自System.Windows.Controls.Panel抽象类;

图1 Panel类及其子类的继承关系图
1. StackPanel示例
XAML代码:
<Window x:Class="LayoutTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="190" Width="300">
<Grid>
<GroupBox Header="What's your occupation?" BorderBrush="Black" Margin="5">
<StackPanel Margin="5" Orientation="Vertical">
<CheckBox Content="A. Teacher"/>
<CheckBox Content="B. Policeman"/>
<CheckBox Content="C. Lawyer"/>
<CheckBox Content="D. Engineer"/>
<CheckBox Content="E. Businessman"/> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Clear" Width="60" Margin="5"/>
<Button Content="Confirm" Width="60" Margin="5"/>
</StackPanel>
</StackPanel>
</GroupBox>
</Grid>
</Window>
显示效果:

2. Grid示例
XAML代码 :
<Window x:Class="LayoutTest.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>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="4"/>
<ColumnDefinition Width="80"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="4"/>
<RowDefinition Height="*"/>
<RowDefinition Height="4"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions> <TextBlock Text="Make your choice:" Grid.Column ="0" Grid.Row="0" VerticalAlignment="Center"/>
<ComboBox Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="4">
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
<ComboBoxItem>D</ComboBoxItem>
</ComboBox>
<TextBox Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="5" BorderBrush="Black"/>
<Button Content="Submit" Grid.Column="2" Grid.Row="4"/>
<Button Content="Clear" Grid.Column="4" Grid.Row="4"/>
</Grid>
</Window>
显示效果:

3、Canvas示例
XAML代码:
<Window x:Class="LayoutTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="140" Width="300">
<Canvas>
<TextBlock Text="User Name:" Canvas.Left="10" Canvas.Top="12" Height="16" Width="70"/>
<TextBox Height="23" Width="180" BorderBrush="Black" Canvas.Left="86" Canvas.Top="9"/>
<TextBlock Text="Password:" Canvas.Left="10" Canvas.Top="40.72" Height="16" Width="70"/>
<TextBox Height="23" Width="180" BorderBrush="Black" Canvas.Left="86" Canvas.Top="38"/>
<Button Content="Confirm" Width="80" Height="22" Canvas.Left="100" Canvas.Top="67"/>
<Button Content="Clear" Width="80" Height="22" Canvas.Left="186" Canvas.Top="67"/>
</Canvas>
</Window>
显示效果:

4、DockPanel示例
XAML代码:
<Window x:Class="LayoutTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="400">
<Grid>
<DockPanel LastChildFill="True">
<TextBox DockPanel.Dock="Top" Width="400" Height="25" BorderBrush="Red">Top</TextBox>
<TextBox DockPanel.Dock="Bottom" Width="400" Height="25" BorderBrush="Red">Bottom</TextBox>
<TextBox DockPanel.Dock="Left" Width="150" BorderBrush="Red">Left</TextBox>
<TextBox DockPanel.Dock="Right" BorderBrush="Red">Right</TextBox>
</DockPanel>
</Grid>
</Window>
显示效果:

5、WrapPanel示例
XAML代码:
<Window x:Class="LayoutTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="400">
<WrapPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
<Button Width="50" Height="50" Content="OK"/>
</WrapPanel>
</Window>
显示效果:

6、GridSplitter示例
XAML代码:
<Window x:Class="LayoutTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions> <TextBox Grid.ColumnSpan="3" BorderBrush="Black"/>
<TextBox Grid.Row="1" BorderBrush="Black"/>
<GridSplitter Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="5" Background="Red" ShowsPreview="True"/>
<TextBox Grid.Row="1" Grid.Column="2" BorderBrush="Black"/>
</Grid>
</Window>
显示效果:

WPF基础学习笔记整理 (四) 布局的更多相关文章
- WPF基础学习笔记整理 (二) XAML
基础知识: XAML:Extensible Application Markup Language, zammel: 用于实例化.NET对象的标记语言: XMAL使用树形逻辑结构描述UI: BAML: ...
- WPF基础学习笔记整理 (九) 资源
基础知识: WPF的资源是一种保管一系列有用对象的简单方法,方便于重用. WPF UI元素的Resources属性,都是继承自FrameworkElement列,且其类型为ResourceDictio ...
- WPF基础学习笔记整理 (八) 命令
基础知识: 命令是应用程序的任务,并跟踪任务是否能够被执行. 命令不包含执行应用程序任务的代码. 命令是比事件更高级的元素.默认的命令目标是当前获得焦点的元素. 良好的Win应用程序,应用程序逻辑不应 ...
- WPF基础学习笔记整理 (七) Binding绑定
基础知识: 数据绑定是一种关系,该关系告诉WPF从源对象提取一些信息,并用这些信息设置目标对象的属性:目标对象始终是依赖属性,而源对象则可以是任何内容. BindingOperations类,提供静态 ...
- WPF基础学习笔记整理 (六) RoutedEvent路由事件
基础知识: 传统的事件模型中,会在消息触发时将消息通过事件传给事件的订阅者(显式的事件订阅),事件订阅者使用事件处理程序来做出响应.事件订阅者必须能够直接访问到事件的宿主(拥有者). 路由事件的事件的 ...
- WPF基础学习笔记整理 (五) DependencyObject & DependencyProperty
参考资料: 1.http://www.cnblogs.com/Zhouyongh/archive/2009/10/20/1586278.html 基础知识: DependencyObject & ...
- WPF基础学习笔记整理 (一)
基础知识: WPF:Windows Presentation Foundation,用于Windows的现代图形显示系统: WPF用于编写应用程序的表示层: 引入“内置硬件加速”和“分辨率无关”: S ...
- WPF基础学习笔记整理 (三) x命名空间
“x命名空间”中x是XAML的首字母,用来引导XAML编译器把XAML代码编译成CLR代码.下边的图片表格列举了该命名空间部分成员及其作用,更多请见URL:https://msdn.microsoft ...
- ASP.Net MVC开发基础学习笔记:四、校验、AJAX与过滤器
一.校验 — 表单不是你想提想提就能提 1.1 DataAnnotations(数据注解) 位于 System.ComponentModel.DataAnnotations 命名空间中的特性指定对数据 ...
随机推荐
- [django]celery_redis探索
celery+redis能做什么及简单原理 能干嘛: 看这里http://yshblog.com/blog/163 https://segmentfault.com/a/119000001565487 ...
- TileMap地图
参考资料: http://8287044.blog.51cto.com/5179921/1045274 TileMap编辑器使用 1.认识TileMap TileMap是一款开源的地图编辑 ...
- XenServer:使用XenCenter开设VPS(多图完整版)
打铁要趁热,咱们接着来玩XenServer.昨天赵容用机房提供的KVM给服务器装了XenServer,今天我们来玩更有意思的:开小鸡.装好XenServer之后,访问我们的服务器IP,就可以看到Xen ...
- Logistic Regression Using Gradient Descent -- Binary Classification 代码实现
1. 原理 Cost function Theta 2. Python # -*- coding:utf8 -*- import numpy as np import matplotlib.pyplo ...
- sql怎么批量替换字段里的字符串的
方法一:varchar和nvarchar类型是支持replace,所以如果你的text不超过8000可以先转换成前面两种类型再使用replace 替换 text ntext 数据类型字段的语句 . 1 ...
- mysql5.7服务器The innodb_system data file 'ibdata1' must be writable导致无法启动服务器
现象如下:The innodb_system data file 'ibdata1' must be writable. 解决方案如下: 1.关闭mysqld进程: 2.删除配置文件中datadir所 ...
- SV中的线程
SV中线程之间的通信可以让验证组件之间更好的传递transaction. SV对verilog建模方式的扩展:1) fork.....join 必须等到块内的所有线程都执行结束后,才能继续执行块后的语 ...
- 20145316许心远《网络对抗》MSF基础应用
20145316许心远<网络对抗>MSF基础应用 实验后回答问题 用自己的话解释什么是exploit,payload,encode. exploit:顾名思义就是攻击嘛,因为是个动词,所以 ...
- Js基础知识1-数组操作全解
数组操作全解 js变量类型 var string; var name = "student",age=12; //underfined.null.boolean.string.nu ...
- 计算两个集合的差集——第六期 Power8 算法挑战赛
第六期Power8大赛 1.1 比赛题目 题目: 计算两个集合的差集: 详细说明: 分别有集合A和B两个大数集合,求解集合A与B的差集(A中有,但B中无的元素),并将结果保存在集合C中,要求集合C中的 ...