StackPanel是非常相似的WrapPanel,但至少有一个重要的区别:StackPanel的不换行的内容。相反,它将内容向一个方向拉伸,允许您将一项一项一项地堆叠在一起。让我们首先尝试一个非常简单的示例,就像我们对 WrapPanel 所做的一样:

<Window x:Class="WpfTutorialSamples.Panels.StackPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="StackPanel" Height="160" Width="300"><StackPanel><Button>Button 1</Button><Button>Button 2</Button><Button>Button 3</Button><Button>Button 4</Button><Button>Button 5</Button><Button>Button 6</Button></StackPanel>
</Window>

您应该注意到的第一件事是 StackPanel 并不真正关心是否有足够的空间容纳内容。它不会以任何方式包装内容,也不会自动为您提供滚动功能(尽管您可以使用 ScrollViewer 控件 - 后面的章节会详细介绍)。

您可能还会注意到 StackPanel 的默认方向是 Vertical,与 WrapPanel 的默认方向是 Horizo​​ntal 不同。但就像 WrapPanel 一样,可以使用 Orientation 属性轻松更改:

<StackPanel Orientation="Horizontal">

您可能会注意到的另一件事是 StackPanel 默认会拉伸其子控件。在垂直对齐的 StackPanel 上,就像第一个示例中的那样,所有子控件都被水平拉伸。在水平对齐的 StackPanel 上,所有子控件都会垂直拉伸,如上所示。StackPanel 通过将其子控件上的 Horizo​​ntalAlignment 或 VerticalAlignment 属性设置为 Stretch 来实现这一点,但如果您愿意,您可以轻松地覆盖它。看看下一个示例,我们使用与前一个示例相同的标记,但这次我们为所有子控件的 VerticalAlignment 属性赋值:

<Window x:Class="WpfTutorialSamples.Panels.StackPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="StackPanel" Height="160" Width="300"><StackPanel Orientation="Horizontal"><Button VerticalAlignment="Top">Button 1</Button><Button VerticalAlignment="Center">Button 2</Button><Button VerticalAlignment="Bottom">Button 3</Button><Button VerticalAlignment="Bottom">Button 4</Button><Button VerticalAlignment="Center">Button 5</Button><Button VerticalAlignment="Top">Button 6</Button></StackPanel>
</Window>

我们使用 Top、Center 和 Bottom 值将按钮放置在一个漂亮的图案中,只是为了踢球。对于垂直对齐的 StackPanel 当然也可以这样做,您可以在子控件上使用 Horizo​​ntalAlignment:

<Window x:Class="WpfTutorialSamples.Panels.StackPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="StackPanel" Height="160" Width="300"><StackPanel Orientation="Vertical"><Button HorizontalAlignment="Left">Button 1</Button><Button HorizontalAlignment="Center">Button 2</Button><Button HorizontalAlignment="Right">Button 3</Button><Button HorizontalAlignment="Right">Button 4</Button><Button HorizontalAlignment="Center">Button 5</Button><Button HorizontalAlignment="Left">Button 6</Button></StackPanel>
</Window>

如您所见,控件仍然从上到下排列,但不是具有相同的宽度,而是每个控件都向左、向右或居中对齐。

WPF 入门教程StackPanel介绍相关推荐

  1. WPF 入门教程WrapPanel介绍

    WrapPanel将定位每个子控件的旁边,另外,水平方向(默认值)或垂直,直到没有更多的空间,在那里将换到下一行,然后继续.当您想要一个垂直或水平列表控件在没有更多空间时自动换行时使用它. 当 Wra ...

  2. WPF入门教程系列四——Dispatcher介绍

    WPF入门教程系列四--Dispatcher介绍 一.Dispatcher介绍 微软在WPF引入了Dispatcher,那么这个Dispatcher的主要作用是什么呢? 不管是WinForm应用程序还 ...

  3. WPF入门教程系列三——Application介绍(续)

    接上文WPF入门教程系列二--Application介绍,我们继续来学习Application 三.WPF应用程序的关闭 WPF应用程序的关闭只有在应用程序的 Shutdown 方法被调用时,应用程序 ...

  4. WPF入门教程(八)--依赖属性(4)(转)

    WPF入门教程(八)--依赖属性(4) 2018年08月27日 11:35:55 weixin_38029882 阅读数:71 我们通过下面的这幅图,简单介绍一下WPF属性系统对依赖属性操作的基本步骤 ...

  5. WPF入门教程(七)---依赖属性(3)(转)

    WPF入门教程(七)---依赖属性(3) 2018年08月24日 08:33:43 weixin_38029882 阅读数:50 四. 只读依赖属性 在以前在对于非WPF的功能来说,对于类的属性的封装 ...

  6. WPF 入门教程(一)

    WPF 入门教程(一) 1.布局规则 1.WPF 窗体中,一个窗体只能持有一个空间,当需要展示多个控件时,则需要首先设置一个容器控件(Container).控件的布局有容器来决定. 2.控件应避免明确 ...

  7. WPF入门教程教学(转载)

    WPF入门教程(1)-基础 https://blog.csdn.net/weixin_38029882/article/details/81867294 WPF入门教程(2)-基础篇 https:// ...

  8. WPF入门教程-转载

    最近为了做炫酷的UI,了解了WPF,之前一直是使用winform的,界面也是古老的不行. 在园里找到了一个大佬以前写的教程,备注一下.按照系列教程走下来,可以直接上手了. 备忘传送门>>& ...

  9. Arduino可穿戴开发入门教程LilyPad介绍

    Arduino可穿戴开发入门教程LilyPad介绍 Arduino输出模块 LilyPad官方共提供了4种输出模块,他们分别是单色LED模块(图1.5).三色LED模块(图1.6).蜂鸣器模块(图1. ...

  10. WPF入门教程系列(二) 深入剖析WPF Binding的使用方法

    同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProperty)只能拥有一个binding. 这一点可以通过设置bindi ...

最新文章

  1. 博客摘录:网络管理员的两天
  2. 日志切割之Logrotate
  3. 用SI 和 DI实现字符串的复制
  4. python oop 继承_oop-在Python中继承方法的文档字符串
  5. 在java.library.path中找不到允许在生产环境中实现最佳性能的基于APR的Apache Tom.....
  6. 5233杨光--第三周实验报告
  7. Docker快速搭建邮件服务器Modoboa
  8. UWP ListView 绑定 单击 选中项 颜色
  9. [开发笔记]-获取歌曲ID3信息
  10. 电脑重置后我的电脑没有了怎么办_笔记本电脑没有声音怎么办?
  11. Vue源码学习1--获取源码和查找入口文件
  12. 什么是Cat.1,Cat.1和4G之间的区别
  13. 小进阶:数据指标体系和数据治理的管理
  14. 机器学习必看书籍推荐
  15. 读书笔记 《一生的计划》
  16. AI产品经理能力模型的重点素质:人文素养和灵魂境界
  17. idea设置java-google-style
  18. android涂鸦实现
  19. C/C++趣味代码-------狸猫换太子
  20. socket聊天室实现-群聊,私聊,好友列表,完整版

热门文章

  1. java在线视频源码_java在线视频会议源代码
  2. C#利用word2007插件实现word转pdf
  3. User-Agent 汇总
  4. JS中Ajax的方法和应用
  5. 计算机组成与体系结构——存储器分层体系结构
  6. 天锐绿盾解密_天锐绿盾签约兴威 助力食品行业信息防泄漏
  7. java 甘特图_[Java教程]JQuery.Gantt(甘特图)开发
  8. 自动控制原理基础学习
  9. Linux镜像最全版本下载网站
  10. App数据抓取(抓包工具使用)