Canvas、StackPanel、WrapPanel、DockPanel和Grid是WPF中主要的5种内建面板,这些面板类都位于System.Windows.Controls命名空间下。

       WrapPanel与StackPanel类似,但是除了会对子元素作栈处理外,当没有足够空间来放一个栈时,它还会把子元素封装在行和列中。这对于显示Item的过渡数目十分有用,会产生一个比普通列表更加有趣的布局,很像Windows资源管理器。与StackPanel医院,WrapPanel没有附加属性来控制元素的位置。它定义了3个控制其行为的属性:

       Orientation——这就像StackPanel中的Orientation属性,唯一不同的是默认值为Horizontal。当Horizontal Orientation看上去类似于Windows资源管理器的缩略图视图:元素是从左向右排列的,然后自上至下自动换行。Vertical Orientation看上去类似于Windows资源管理器的列表视图:元素是从上向下排列的,然后从左至右自动换行。

       ItemHeight——所有子元素都一致的高度。每个子元素填充高度的方式取决于它的VerticalAlignment属性、Height属性等。任何比ItemHeight高的元素都将被截断。

       ItemWidth——所有子元素都一致的宽度。每个子元素填充高度的方式取决于它的VerticalAlignment属性、Width属性等。任何比ItemWidth高的元素都将被截断。

       将上篇文章中的StackPanel改成WrapPanel,即可得到如下效果:

<Window x:Class="WPF5.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">
    <WrapPanel>
       
        <WrapPanel ItemHeight="50" ItemWidth="50">
            <Button Background="Red">NoScaling</Button>
            <Button Background="Orange">
                X
            </Button>
            <Button Background="Yellow">
                X+Y
            </Button>
            <Button Background="Lime">
                Y
            </Button>
          
        </WrapPanel>
    </WrapPanel>
</Window>

     当WrapPanel有充足的空间,但ItemHeight和ItemWidth没有设置时,WrapPanel看上去与StackPanel没什么两样。

     WrapPanel是最常用作Items控件的项目面板。例如,如果你想实现选中功能,使用一个ListBox但把它的默认面板换成WrapPanel就可以做到。如下所示:

    <ListBox>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel></WrapPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            ……
        </ListBox>

posted on 2011-03-18 10:56  Jeallyn  阅读(22539)  评论(3编辑  收藏  举报