如何将用户控件数组放在Grid的列中

时间:2010-02-17 06:44:12

标签: silverlight

您好我有一系列员工数据,包括照片,说明,职位名称,薪水。我必须在单独的面板控制结构中显示每个员工数据,这意味着如果我有10名员工,那么将有10个面板。我必须在一个有两列的网格中显示这些面板。每列中面板的宽度也会根据主页面大小而变化。

1 个答案:

答案 0 :(得分:0)

为什么不用单独的userControl表示员工数据,然后使用Wrap面板显示员工。这种方法可以处理不同数量的员工。

为了更好地说明我的想法,这里有一些代码:

MainPage有一个网格,其中有一个填充可用空间的WrapPanel。

MainPage.xaml中

<UserControl 
    x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ct="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    mc:Ignorable="d">

    <Grid x:Name="LayoutRoot" Background="WhiteSmoke">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
            <ColumnDefinition Width="200"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="200"/>
            <RowDefinition/>
            <RowDefinition Height="200"/>
        </Grid.RowDefinitions>

        <ct:WrapPanel x:Name="panel1" Grid.Row="1" Grid.Column="1" 
                      Background="Aqua" VerticalAlignment="Stretch"
                      HorizontalAlignment="Stretch"/>

    </Grid>

</UserControl>

MainPage.xaml.cs中

  public partial class MainPage : UserControl
    {
        public MainPage ()
        {
            InitializeComponent();

            this.DataContext = this;
            this.Loaded += new RoutedEventHandler( MainPage_Loaded );
        }

        void MainPage_Loaded ( object sender, RoutedEventArgs e )
        {
            for ( int x = 0; x <= 10; x++ )
            {
                panel1.Children.Add( new ChildControl() );
            }
        }
    }

我正在添加如下定义的ChildWindow控件,以显示WrapPanel如何使其子项的表示适应可用空间。

ChildWindow.xaml

<UserControl 
    x:Class="SilverlightApplication2.ChildControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Width="100"
    Height="100">

    <Grid x:Name="LayoutRoot" Background="PowderBlue">
        <TextBlock Text="ChildControl"/>
    </Grid>
</UserControl>

如果我错过了你的观点,请给予更多澄清。