当前在我的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>
我不那么在意最终的结果如何看待在屏幕上。所有这一切的原因是客户想要在单独的页面上打印每一行,所以我将创建多个数据网格并将每个数据网格独立传递给可视化打印机(以PrintVisual和for-loop打印每个“datagrid”) – JSchwartz 2013-04-20 03:04:58
@JSchwartz发布相关代码和XAML。否则这是所有的猜测。 – 2013-04-20 06:48:16
@debracey关于ASP中继器控制的好主意。除了它在WPF中完全不相关。我希望我能减少评论。 – 2013-04-20 06:48:46