我有一个在设计模式下看起来很好的ucReceipt.xml,或者在打开窗口http://prntscr.com/5xl11h时看起来很好看:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Receipt.ReceiptItems}"
IsReadOnly="True" CanUserSortColumns="False" CanUserResizeColumns="False"
RowHeaderWidth="0" Grid.Row="4">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ItemNo}" Header="No" Width="0.5*" />
<DataGridTextColumn Binding="{Binding cloth.Name}" Header="Description" Width="*" />
<DataGridTextColumn Binding="{Binding Quantity}" Header="Qty" Width="auto">
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.HorizontalAlignment" Value="Stretch" />
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding UnitPrice}" Header="Price" Width="auto">
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.HorizontalAlignment" Value="Stretch" />
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding SubTotal}" Header="Amount" Width="auto">
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.HorizontalAlignment" Value="Stretch" />
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
但是,当我尝试使用以下代码进行PrintVisual时,DataGridColumns不能很好地调整大小http://prntscr.com/5xl1ds
if (SelectReceiptWindow.SelectedReceipt != null)
{
Receipt = SelectReceiptWindow.SelectedReceipt;
if (ReceiptWindow == null)
ReceiptWindow = new ReceiptViewModel();
ReceiptWindow.Receipt = Receipt;
ReceiptWindow.IsOpen = true;
// FIXME!!! remove after implementing CloseCmd
if (ReceiptWindow.IsOpen)
ReceiptWindow.IsOpen = false;
switch(ReceiptWindow.ReceiptActions)
{
case 1: // print Receipt
System.Windows.Controls.PrintDialog dlg = new System.Windows.Controls.PrintDialog();
ucReceipt r = new ucReceipt();
r.Width = 300;
r.Height = 600;
r.DataContext = ReceiptWindow;
dlg.ShowDialog();
dlg.PrintVisual((System.Windows.Media.Visual)r, "Receipt");
break;
}
}
有什么想法吗?