1

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

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

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

为每一行视图
创建新的数据网格
分配行作为 itemsource 绑定

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

原因 - 客户希望在单独的 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>
4

1 回答 1

1

你可以形成一个list of list,ViewRecipe然后把它传递Itemssource给to ItemsControl,也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>
于 2013-04-20T21:23:28.653 回答