问题
I have a ucReceipt.xml that looks fine in design mode or even it looks good while Opening the window 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>
However, when I try to PrintVisual with the following codes, DataGridColumns don't resize well 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;
}
}
Any idea?
来源:https://stackoverflow.com/questions/28163775/wpf-datagridcolumnwidth-doesnt-work-well-when-printvisual