2013-04-20 49 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

相关问题