• 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基础学习笔记整理 (四) 布局的更多相关文章

  1. WPF基础学习笔记整理 (二) XAML

    基础知识: XAML:Extensible Application Markup Language, zammel: 用于实例化.NET对象的标记语言: XMAL使用树形逻辑结构描述UI: BAML: ...

  2. WPF基础学习笔记整理 (九) 资源

    基础知识: WPF的资源是一种保管一系列有用对象的简单方法,方便于重用. WPF UI元素的Resources属性,都是继承自FrameworkElement列,且其类型为ResourceDictio ...

  3. WPF基础学习笔记整理 (八) 命令

    基础知识: 命令是应用程序的任务,并跟踪任务是否能够被执行. 命令不包含执行应用程序任务的代码. 命令是比事件更高级的元素.默认的命令目标是当前获得焦点的元素. 良好的Win应用程序,应用程序逻辑不应 ...

  4. WPF基础学习笔记整理 (七) Binding绑定

    基础知识: 数据绑定是一种关系,该关系告诉WPF从源对象提取一些信息,并用这些信息设置目标对象的属性:目标对象始终是依赖属性,而源对象则可以是任何内容. BindingOperations类,提供静态 ...

  5. WPF基础学习笔记整理 (六) RoutedEvent路由事件

    基础知识: 传统的事件模型中,会在消息触发时将消息通过事件传给事件的订阅者(显式的事件订阅),事件订阅者使用事件处理程序来做出响应.事件订阅者必须能够直接访问到事件的宿主(拥有者). 路由事件的事件的 ...

  6. WPF基础学习笔记整理 (五) DependencyObject & DependencyProperty

    参考资料: 1.http://www.cnblogs.com/Zhouyongh/archive/2009/10/20/1586278.html 基础知识: DependencyObject & ...

  7. WPF基础学习笔记整理 (一)

    基础知识: WPF:Windows Presentation Foundation,用于Windows的现代图形显示系统: WPF用于编写应用程序的表示层: 引入“内置硬件加速”和“分辨率无关”: S ...

  8. WPF基础学习笔记整理 (三) x命名空间

    “x命名空间”中x是XAML的首字母,用来引导XAML编译器把XAML代码编译成CLR代码.下边的图片表格列举了该命名空间部分成员及其作用,更多请见URL:https://msdn.microsoft ...

  9. ASP.Net MVC开发基础学习笔记:四、校验、AJAX与过滤器

    一.校验 — 表单不是你想提想提就能提 1.1 DataAnnotations(数据注解) 位于 System.ComponentModel.DataAnnotations 命名空间中的特性指定对数据 ...

随机推荐

  1. [django]form不清空问题解决

    https://www.cnblogs.com/OldJack/p/7118396.html 有时候提交表单后,发现某个字段写错了,但是form的其他字段竟然被清空,这个万万不能接受.所有django ...

  2. 深入理解python之二——python列表和元组

    从一开始学习python的时候,很多人就听到的是元组和列表差不多,区别就是元组不可以改变,列表可以改变. 从数据结构来说,这两者都应当属于数组,元组属于静态的数组,而列表属于动态数组.稍后再内存的分配 ...

  3. Redis日常使用随笔

    Redis常用配置:1.采用master与slave互为主从的方式,两条重要命令:redis 127.0.0.1:6379> SLAVEOF NO ONEredis 127.0.0.1:6379 ...

  4. pandas使用drop_duplicates去除DataFrame重复项

    DataFrame中存在重复的行或者几行中某几列的值重复,这时候需要去掉重复行,示例如下: data.drop_duplicates(subset=['A','B'],keep='first',inp ...

  5. 在Win7系统下, 使用VS2015 打开带有日文注释程序出现乱码的解决方案

    在Win7系统下, 使用VS2015 打开带有日文注释程序出现乱码的解决方案 下载: apploc.msi (下载地址:http://microsoft-applocale.software.info ...

  6. 渗透msf工具中andorid被控端的实现

    msf中andoird端的实现代码一共只有8k 核心代码就是下载者. 下载dex,动态执行dex.

  7. Android下基于线程池的网络访问基础框架

    引言 现在的Android开发很多都使用Volley.OkHttp.Retrofit等框架,这些框架固然有优秀的地方(以后会写代码学习分享),但是我们今天介绍一种基于Java线程池的网络访问框架. 实 ...

  8. 更高效的MergeSort--稍微优化

    0. 简介 本文简要介绍一下比传统MergeSort更高效的算法,在原来的算法Merge基础上,少发生一半拷贝.欢迎探讨,感谢阅读. 原文链接如下:http://loverszhaokai.com/p ...

  9. Docker深入浅出3-容器管理

    docker客户端非常简单,我们可以直接输入docker命令来查看Docker客户端所有的命令项 [root@admin-fxr ~]# docker Usage: docker COMMAND A ...

  10. CXF创建webservice客户端和服务端

    转 一.CXF的介绍 Apache CXF是一个开源的WebService框架,CXF大大简化了Webservice的创建,同时它继承了XFire的传统,一样可以和spring天然的进行无缝的集成.C ...