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 命名空间中的特性指定对数据 ...
随机推荐
- eclipse向svn提交代码的时候忽略部分资源配置
eclipse向svn提交代码的时候有 .settings, .project, .classpath, target等不需要上传,所以在eclipse中配置一下就不会显示了,方法如下图:
- 两个list对应元素相加
a=[1,2,3] b=[4,5,6] 现将list a与 list b按位相加,其结果为[5,7,9] 方法一: c=[a[i]+b[i] for i in range(min(len(a),len ...
- 筛选DataTable中的数据
DataTable dt = bll.GetTable(); //查询数据 DataTable newdt = new DataTable(); //一个新的table来保存筛选的记录 newdt = ...
- OO第三次阶段性总结
一.规格化设计的历史以及人们重视的原因 发展历史 从20世纪60年代开始,就存在着许多不同的形式规格说明语言和软件开发方法.在形式规格说明领域一些最主要的发展过程列举如下: 1969-1972 C.A ...
- 借root之名,行流氓之实,劝告,root需谨慎
20160425++++++ 今日再回头看这篇文章,貌似有点偏激了一点,不过xda论坛上有个疑似kingroot开发团队的用户说明了kingroot确实对supersu做了限制,说是supersu在替 ...
- Class类的特性(上)
Class类的特性(上) ES6 的类,完全可以看作ES5构造函数的另一种写法. class Point { // ... } typeof Point // "function" ...
- javascript的Object对象的defineProperty和defineProperties
Object的属性 查看官网:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Obje ...
- SVA描述(一)
SystemVerilog Assertion(SVA):是一种描述性的语言,可以很容易的描述时序相关的情况,所以主要用在协议检查和协议覆盖.SVA在systemverilog仿真器中的 调度区间在R ...
- CodeForces - 946D Timetable (分组背包+思维)
题意 n天的课程,每天有m个时间单位.若时间i和j都有课,那么要在学校待\(j-i+1\)个时间.现在最多能翘k节课,问最少能在学校待多少时间. 分析 将一天的内容视作一个背包的组,可以预处理出该天内 ...
- python练习题-简单方法判断三个数能否组成三角形
python简单方法判断三个数能否组成三角形 #encoding=utf-8 import math while True: str=raw_input("please input thre ...