WPF DataGridColumnWidth doesn't work well when PrintVisual

倖福魔咒の 提交于 2019-12-08 04:00:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!