2010-02-17 48 views
0

嗨我有一組員工數據,其中包括照片,說明,職位名稱,薪水。我必須將每個員工的數據顯示在獨立的面板控制結構中,這意味着如果我有10名員工,則會有10個面板。我必須在有兩列的網格中顯示這些面板。每列中的面板寬度也會根據主頁面大小而變化。如何將用戶控件的數組放入Grid的列

+0

爲什麼兩列?如果有更多的員工能夠適應窗口,會發生什麼? – AnthonyWJones 2010-02-17 08:36:40

回答

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> 

如果我錯過了你的觀點,請給一些更多的澄清。

+0

我的確完全一樣。我想利用主頁的整個空間,但使用水平方向的包裝面板,我無法這樣做。如果主頁面的大小發生變化,主頁面右側的空間將會浪費。 – jolly 2010-02-18 08:59:00

+0

有沒有其他的方式,而不是使用包裝面板? – jolly 2010-02-18 09:02:08

+0

我不想在layoutRoot中將列寬設置爲固定值。列的寬度將根據頁面而有所不同,因此MyCustomControl的寬度應根據不同而不同。 – jolly 2010-02-18 14:45:51

相關問題