1

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?

alkk
  • 331
  • 5
  • 11
  • I dunno the root cause. since the width of the receipt is fixed (300). if you set the width of each column explicitly, it should print normally, don't know if that is an option. – kennyzx Jan 27 '15 at 13:15
  • Yes, if they're with explicit widths, it worked fine. But I'm trying to see if there's a way to auto size the width, then the height... – alkk Jan 27 '15 at 14:02

0 Answers0