2013-04-20 138 views
1

當前在我的XAML文件中定義了一個綁定(項目源)數據集合(稱爲視圖)的單個數據網格,並且這個數據網格顯示在一個大表中(如預期並且完美工作)。每行ItemsSource綁定一個datagrid

但是我現在需要的是創建一個DataGrid PER行,從而結束了所有包含單行數據的多個數據網格。

我能想到的唯一的事情就是:
- 動態創建DataGrid中的代碼(在某種程度上),並從XAML
刪除 - 與特定行填充這些動態創建DataGrid中的的的ItemSource中的數據,例如(僞代碼):

對於每個視行
創建新數據網格
分配行作爲結合的ItemSource

有沒有人有更好的建議?這甚至可以按我提出的方式完成嗎?有更好/更簡單的方法嗎?

原因 - 客戶想要在單獨的頁面上打印每一行,所以我將創建多個數據網格並將每個數據網格獨立傳遞給可視化打印機以實現此目的。

CODE:

// this is the datasource, essentially we want to show one recipe per printed page (so per datagrid) 
List<ViewRecipe> View 

XAML:

<DataGrid ItemsSource="{Binding View}" 
      AutoGenerateColumns="False" 
      Height="Auto" 
      CanUserAddRows="False" 
      CanUserDeleteRows="False" 
      CanUserResizeColumns="False" 
      CanUserResizeRows="False" 
      CanUserReorderColumns="False" 
      IsReadOnly="True" 
      GridLinesVisibility="None"> 
    <DataGrid.ColumnHeaderStyle> 
    <Style TargetType="{x:Type DataGridColumnHeader}"> 
     <Setter Property="FontWeight" 
       Value="Bold" /> 
     <Setter Property="FontSize" 
       Value="12" /> 
    </Style> 
    </DataGrid.ColumnHeaderStyle> 
    <DataGrid.Columns> 
    <DataGridTextColumn Header="Type" 
         Width="200" 
         FontSize="12" 
         Binding="{Binding Path=Name}" /> 
    <DataGridTemplateColumn Header="Ingredients" 
          Width="*"> 
     <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <DataGrid AutoGenerateColumns="False" 
        CanUserAddRows="False" 
        CanUserDeleteRows="False" 
        CanUserResizeColumns="False" 
        CanUserResizeRows="False" 
        CanUserReorderColumns="False" 
        IsReadOnly="True" 
        GridLinesVisibility="None" 
        ItemsSource="{Binding Ingredients}"> 
      <DataGrid.ColumnHeaderStyle> 
       <Style TargetType="{x:Type DataGridColumnHeader}"> 
       <Setter Property="FontWeight" 
         Value="Bold" /> 
       <Setter Property="FontSize" 
         Value="12" /> 
       </Style> 
      </DataGrid.ColumnHeaderStyle> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Ingredients" 
            Width="*" 
            FontSize="12" 
            Binding="{Binding Path=IngredientName}" /> 
       <DataGridTextColumn Header="Quantite" 
            Width="*" 
            FontSize="12" 
            Binding="{Binding Path=Qty}" /> 
      </DataGrid.Columns> 
      </DataGrid> 
     </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 
+0

我不那麼在意最終的結果如何看待在屏幕上。所有這一切的原因是客戶想要在單獨的頁面上打印每一行,所以我將創建多個數據網格並將每個數據網格獨立傳遞給可視化打印機(以PrintVisual和for-loop打印每個「datagrid」) – JSchwartz 2013-04-20 03:04:58

+0

@JSchwartz發佈相關代碼和XAML。否則這是所有的猜測。 – 2013-04-20 06:48:16

+0

@debracey關於ASP中繼器控制的好主意。除了它在WPF中完全不相關。我希望我能減少評論。 – 2013-04-20 06:48:46

回答

1

可以形成ViewRecipe列表的列表,然後把它作爲ItemssourceItemsControl,這ItemsTemplate是你DataGrid

代碼:

List<List<ViewRecipe>> ViewExtended = new List<List<ViewRecipe>>(); 

foreach (var r in View) 
{ 
    var l = new List<ViewRecipe>(); 
    ViewExtended.Add(l); 
} 

XAML

<ItemsControl ItemsSource="{Binding Path=ViewExtended}"> 
<ItemsControl.ItemTemplate> 
    <DataTemplate> 
    <DataGrid ItemsSource="{Binding}"> 

    ...... 

    </DataGrid> 
    </DataTemplate 
<ItemsControl.ItemTemplate> 
</ItemsControl> 
+0

這不會編譯:錯誤 foreach語句不能對'System.Windows.Controls.DataGrid'類型的變量進行操作,因爲'System.Windows.Controls.DataGrid'不包含「GetEnumerator'的公共定義 – JSchwartz 2013-04-25 03:35:45

+0

@JSchwartz,我給你一個想法如何將數據拆分爲多個數據網格(順便說一句,我在代碼中檢查了它)。在我的例子中,沒有'foreach'循環遍歷'DataGrid' – shibormot 2013-04-25 04:13:32