半岛权威十大直营(官方)网站

WrapPanel组件的基本特性以及应用实例

转帖|其它|编辑:郝浩|2011-01-06 16:56:18.000|阅读 750 次

概述:WrapPanel组件作用是从左至右或从上至下依次安排位于其中的元素的位置,当元素超过该组件边缘时,它们将会被自动安排至下一行或列。该组件一般用于文本布局、拾色器、图片选择等。本文将为大家介绍该组件的基本特性以及应用实例。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  WrapPanel组件作用是从左至右或从上至下依次安排位于其中的元素的位置,当元素超过该组件边缘时,它们将会被自动安排至下一行或列。该组件一般用于文本布局、拾色器、图片选择等。本文将为大家介绍该组件的基本特性以及应用实例。

  组件所在命名空间:

  System.Windows.Controls

  组件常用属性:

  ItemHeight:获取或设置包含在WrapPanel组件中的每一个项目布局区域的高度。

  ItemWidth:获取或设置包含在WrapPanel组件中的每一个项目布局区域的宽度。

  Orientation:获取或设置子元素被安排布局的方向。

  实例:

  说明:WrapPanel组件还可以作为item组件面板模板使用,常作为ListBox等组件的item组件面板模板。具体可以参考园友Nasa的一篇文章:巧用WrapPanel为ItemsControl绑定数据

详细的说明在代码注释中给出。

  MainPage.xaml文件代码

<UserControl

    xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x=&quot;//schemas.microsoft.com/winfx/2006/xaml";

    xmlns:d="//schemas.microsoft.com/expression/blend/2008" xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d" xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" x:Class="SilverlightClient.MainPage"

    d:DesignWidth="320" d:DesignHeight="480">

    <Grid x:Name="LayoutRoot" Width="320" Height="480" Background="White">

        <Slider x:Name="HSlider" Height="23" Margin="8,202,8,0" VerticalAlignment="Top" Maximum="100" Value="100"/>

        <Border x:Name="theContainer" Margin="8,8,8,0" BorderBrush="Black" BorderThickness="1" Height="190" VerticalAlignment="Top">

    &nbsp;       <controlsToolkit:WrapPanel x:Name="wp" Margin="-1"/>

        </Border>

        <Border Margin="8,234,8,52" BorderBrush="Black" BorderThickness="1">

        ;    <controlsToolkit:WrapPanel x:Name="wp2" Margin="-1"/>

        </Border>

        <CheckBox x:Name="chkHorizontal" Height="22" HorizontalAlignment="Left" Margin="7,0,0,24" VerticalAlignment="Bottom" Width="95" Content="是否水平" FontSize="16"/>

    </Grid>

</UserControl>

  MainPage.xaml.cs文件代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

namespace SilverlightClient

{

    public partial class MainPage : UserControl

    {

     &nbsp; &nbsp;public MainPage()

        {

         &nbsp;  InitializeComponent();

    &nbsp;       //注册事件触发

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

          &nbsp; this.HSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(HSlider_ValueChanged);

            this.chkHorizontal.Click += new RoutedEventHandler(chkHorizontal_Click);

        }

        //设置WrapPanel的布局方向

   &nbsp;    void chkHorizontal_Click(object sender, RoutedEventArgs e)

        {

 ;           if (chkHorizontal.IsChecked == true)

         &nbsp;  {

           &nbsp;    wp2.Orientation = Orientation.Horizontal;

  &nbsp;      &nbsp;  }

        ;    else

         &nbsp;  {

                wp2.Orientation = Orientation.Vertical;

    &nbsp;       }

 

        }

        void HSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)

        {

           

            wp.Width = theContainer.ActualWidth * HSlider.Value / 100.0;

        }

   &nbsp; &nbsp;  void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

          ;  //wp的内容添加

      &nbsp;    ; for (int i = 0; i < 20; i++)

          ;  {

                wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.Black) });

                wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.Blue) });

    &nbsp;           wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.Brown) });

          &nbsp;     wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.Cyan) });

                wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.DarkGray) });

              &nbsp; wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.Green) });

                wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.Magenta) });

                wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.Purple) });

                wp.Children.Add(new Rectangle() { Height = 20, Width = 20, Fill = new SolidColorBrush(Colors.Yellow) });

           ; }

            //wp2的内容添加

      &nbsp;     for(int i=0;i<20;i++)

      &nbsp;     {

           ;     string s = "测试文字!";

                TextBlock tb = new TextBlock();

    &nbsp;           tb.Text = s;

                tb.FontSize = 14;

   &nbsp;            wp2.Children.Add(tb);

        &nbsp;&nbsp;  }

            chkHorizontal.IsChecked = true;

        }

    }

}

  最终效果图:

  

 

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@wqylolg.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP