每行ItemsSource绑定一个数据网格

时间:2013-04-20 02:55:21

标签: c# wpf xaml

目前在我的XAML文件中定义了一个数据网格,它绑定(itemsource)一个数据集合(称为视图),它显示在一个大表中(正如预期的那样完美地工作)。

然而,我现在需要的是创建一个数据网格PER行,从而最终得到许多包含单行数据的数据网格。

我唯一能想到的是:
- 在代码中动态创建数据网格(以某种方式)并将其从XAML中删除 - 使用特定的数据行填充每个动态创建的数据网格的itemsource,例如(伪代码):

  
    每行视图的


    创建新的数据网格     将row指定为itemsource binding

  

有没有人有更好的建议?这甚至可以按我建议的方式完成吗?有更好/更简单的方法吗?

原因 - 客户希望在单独的PAGE上打印每一行,这样我就会创建许多数据网格,并将每个数据网格独立传递给printvisual以实现此目的。

代码:

// 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>

1 个答案:

答案 0 :(得分:1)

您可以列出ViewRecipe列表,然后将其作为Itemssource传递给ItemsControlItemsTemplate是您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>
相关问题