书名:WPF专业编程指南
作者:李应保
出版社:电子工业出版社
出版时间:2010-01
ISBN:9787121100116
一、WrapPanel
- WrapPanel是和StackPanel最相近的一个控制面板,StackPanel把其中的UI元素按行或列排列,而WrapPanel则可根据其中UI元素的尺寸和其自身可能的大小自动地把其中的UI元素排列到下一行或下一列。Windows操作系统中的文件管理器就是这样的例子,当选择图标显示模式时,文件管理器将根据视窗的宽度和文件图标的大小,自动布置文件在其中的位置。
二、示例
- 在下面的例子中,在WrapPanel中放入了七个矩形图形,每个图形的宽为70、高为40。
为了更好地展示WrapPanel面板的功能,设置了Window的MinHeight和MaxWidth两个属性,这两个属性表示视窗所能改变的最小高度和宽度。
<Window x:Class="Yingbao.Chapter3.WrapPanelLayout"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WrapPanel 的例子" Height="270" Width="212" MinHeight="250"
MaxWidth="555" >
<WrapPanel x:Name="ButtonPanel" Background="Gainsboro">
<Rectangle Width="70" Height ="40">
<Rectangle.Fill>
<SolidColorBrush Color="Red" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Width="70" Height ="40">
<Rectangle.Fill>
<SolidColorBrush Color="Aqua" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Width="70" Height ="40">
<Rectangle.Fill>
<SolidColorBrush Color="Blue" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Width="70" Height ="40">
<Rectangle.Fill>
<SolidColorBrush Color="LightGreen" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Width="70" Height ="40">
<Rectangle.Fill>
<SolidColorBrush Color="Yellow" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Width="70" Height ="40">
<Rectangle.Fill>
<SolidColorBrush Color="Navy" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Width="70" Height ="40">
<Rectangle.Fill>
<SolidColorBrush Color="Gold" />
</Rectangle.Fill>
</Rectangle>
</WrapPanel >
</Window>
-
上面这小段XAML程序的运行结果如图3-6所示。
可用鼠标调整视窗的大小,当视窗的宽度小于其中矩形宽度的总和时,排在右面的矩形会自动排列到下一行;
当视窗的宽度大于其中矩形宽度的总和时,排在下一行最左边的矩形会自动进入上一行。
当视窗的宽度只能放置1个矩形时,WrapPanel的效果和StackPanel的效果一样,如图3-7所示。
图3-6 WrapPanel示例
三、三个属性
- WrapPanel中有三个属性ItemWidth、ItemHeight和Orientation,这三个属性来安排其中UI元素的位置:
