當前在我的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