XAML中的FlowDocument在打印时不响应

时间:2017-06-15 03:38:17

标签: c# wpf xaml printing

问题在于,当我打印FlowDocument时,它打印的内容与Window显示的内容相同,而不是FlowDocument本身。

这是XAML:

<Window x:Class="POS.DailySalesReport"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:POS"
        mc:Ignorable="d"
        Height="500"
        Width="816"
        Loaded="Window_Loaded"
        WindowStartupLocation="CenterScreen">
    <Grid>
        <FlowDocumentPageViewer x:Name="flwView">
            <FlowDocument x:Name="flwDoc"
                          TextAlignment="Center"
                          ColumnWidth="{Binding ElementName=flwView, Path=ActualWidth}">
                <BlockUIContainer>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid x:Name="grdDetails"
                              Grid.Row="0"
                              Grid.Column="0"
                              Margin="0,0,0,30">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="2*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Ellipse Grid.Row="0"
                                     Grid.Column="0"
                                     Height="90"
                                     Width="90"
                                     HorizontalAlignment="Center"
                                     VerticalAlignment="Center">
                                <Ellipse.Fill>
                                    <ImageBrush x:Name="imgUser" />
                                </Ellipse.Fill>
                            </Ellipse>
                            <Grid Grid.Row="0"
                                  Grid.Column="1"
                                  Margin="10, 0, 0, 0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="2*" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock x:Name="lblCompanyName"
                                           Grid.Row="0"
                                           Grid.Column="0"
                                           Text="COMPANY NAME"
                                           FontFamily="{StaticResource RobotoRegular}"
                                           FontSize="18"
                                           VerticalAlignment="Center"
                                           HorizontalAlignment="Left" />
                                <TextBlock x:Name="lblCompanyAddress"
                                           Grid.Row="1"
                                           Grid.Column="0"
                                           Text="Business Address"
                                           FontFamily="{StaticResource RobotoRegular}"
                                           FontSize="12"
                                           VerticalAlignment="Center"
                                           HorizontalAlignment="Left" />
                                <TextBlock x:Name="lblCompanyNumber"
                                           Grid.Row="2"
                                           Grid.Column="0"
                                           Text="Contact Number"
                                           FontFamily="{StaticResource RobotoRegular}"
                                           FontSize="12"
                                           VerticalAlignment="Center"
                                           HorizontalAlignment="Left" />
                                <TextBlock x:Name="lblCompanyTIN"
                                           Grid.Row="3"
                                           Grid.Column="0"
                                           Text="TIN Number"
                                           FontFamily="{StaticResource RobotoRegular}"
                                           FontSize="12"
                                           VerticalAlignment="Center"
                                           HorizontalAlignment="Left" />
                            </Grid>
                            <StackPanel Grid.Row="0"
                                        Grid.Column="2">
                                <TextBlock x:Name="lblReportType"
                                           FontFamily="{StaticResource RobotoBold}"
                                           FontSize="12"
                                           HorizontalAlignment="Right" />
                                <TextBlock x:Name="lblReportDate"
                                           FontFamily="{StaticResource RobotoRegular}"
                                           FontSize="12"
                                           HorizontalAlignment="Right" />
                            </StackPanel>
                        </Grid>
                        <ListView x:Name="lstView"
                                  Grid.Row="1"
                                  Grid.Column="0"
                                  BorderThickness="0"
                                  SelectionMode="Single"
                                  Width="{Binding ElementName=flwDoc, Path=ActualWidth}">
                            <ListView.Resources>
                                <Style TargetType="{x:Type GridViewColumnHeader}">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <SolidColorBrush Color="White" />
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="Foreground">
                                        <Setter.Value>
                                            <SolidColorBrush Color="Black" />
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="FontSize"
                                            Value="11" />
                                    <Setter Property="FontFamily"
                                            Value="{StaticResource RobotoMedium}" />
                                    <Setter Property="Height"
                                            Value="35" />
                                    <Setter Property="BorderThickness"
                                            Value="0, 1, 0, 1" />
                                    <Setter Property="BorderBrush">
                                        <Setter.Value>
                                            <SolidColorBrush Color="Black" />
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                                <Style TargetType="{x:Type ListView}">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <SolidColorBrush Color="White" />
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                                <Style TargetType="{x:Type ListViewItem}">
                                    <Setter Property="HorizontalContentAlignment"
                                            Value="Stretch" />
                                    <Setter Property="Foreground">
                                        <Setter.Value>
                                            <SolidColorBrush Color="Black"
                                                             Opacity="0.8" />
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="FontFamily"
                                            Value="{StaticResource RobotoMedium}" />
                                    <Setter Property="FontSize"
                                            Value="17" />
                                    <Setter Property="Background"
                                            Value="White" />
                                    <Setter Property="Padding"
                                            Value="0, 10, 0, 10" />
                                    <Setter Property="Margin"
                                            Value="0, 0, 0, 10" />
                                    <Style.Resources>
                                        <!-- Foreground for Selected ListViewItem -->
                                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
                                                         Color="Black"
                                                         Opacity="0.8" />
                                        <!-- Background for Selected ListViewItem -->
                                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                                         Color="White" />
                                        <!-- Foreground for Inactive Selected ListViewItem -->
                                        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
                                                         Color="Black"
                                                         Opacity="0.8" />
                                        <!-- Background for Inactive Selected ListViewItem -->
                                        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                                                         Color="White" />
                                    </Style.Resources>
                                </Style>
                            </ListView.Resources>
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn x:Name="lstHeader1"
                                                    Header="Description">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding ProductDescription}"
                                                           TextAlignment="Center"
                                                           FontFamily="{StaticResource RobotoRegular}"
                                                           FontSize="12"
                                                           TextWrapping="Wrap" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn x:Name="lstHeader2"
                                                    Header="Supplier">

                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Supplier}"
                                                           TextAlignment="Center"
                                                           FontFamily="{StaticResource RobotoRegular}"
                                                           FontSize="12"
                                                           TextWrapping="Wrap" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn x:Name="lstHeader3"
                                                    Header="Sold">

                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding ProductSold}"
                                                           TextAlignment="Center"
                                                           FontFamily="{StaticResource RobotoRegular}"
                                                           FontSize="12"
                                                           TextWrapping="Wrap" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn x:Name="lstHeader4"
                                                    Header="Cost Price">

                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding ProductCost}"
                                                           TextAlignment="Center"
                                                           FontFamily="{StaticResource RobotoRegular}"
                                                           FontSize="12"
                                                           TextWrapping="Wrap" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn x:Name="lstHeader5"
                                                    Header="Retail Price">

                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding ProductRetail}"
                                                           TextAlignment="Center"
                                                           FontFamily="{StaticResource RobotoRegular}"
                                                           FontSize="12"
                                                           TextWrapping="Wrap" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn x:Name="lstHeader6"
                                                    Header="Total(Qty * Retail Price)">

                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding TotalRetail}"
                                                           TextAlignment="Center"
                                                           FontFamily="{StaticResource RobotoRegular}"
                                                           FontSize="12"
                                                           TextWrapping="Wrap" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </GridView>
                            </ListView.View>
                        </ListView>
                        <Grid Grid.Row="2"
                              Grid.Column="0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="2*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid Grid.Row="0"
                                  Grid.Column="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Row="0"
                                           Grid.Column="0"
                                           Text="Total Items Sold"
                                           HorizontalAlignment="Right"
                                           VerticalAlignment="Center"
                                           FontSize="10"
                                           FontFamily="{StaticResource RobotoBold}" />
                                <TextBlock x:Name="lblTotalSold"
                                           Grid.Row="0"
                                           Grid.Column="1"
                                           Text="0"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           FontSize="10"
                                           FontFamily="{StaticResource RobotoRegular}" />

                                <TextBlock Grid.Row="1"
                                           Grid.Column="0"
                                           Text="Total Cost Amount"
                                           HorizontalAlignment="Right"
                                           VerticalAlignment="Center"
                                           FontSize="10"
                                           FontFamily="{StaticResource RobotoBold}" />
                                <TextBlock x:Name="lblTotalCost"
                                           Grid.Row="1"
                                           Grid.Column="1"
                                           Text="0"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           FontSize="10"
                                           FontFamily="{StaticResource RobotoRegular}" />

                                <TextBlock Grid.Row="2"
                                           Grid.Column="0"
                                           Text="Total Retail Amount"
                                           HorizontalAlignment="Right"
                                           VerticalAlignment="Center"
                                           FontSize="10"
                                           FontFamily="{StaticResource RobotoBold}" />
                                <TextBlock x:Name="lblTotalRetail"
                                           Grid.Row="2"
                                           Grid.Column="1"
                                           Text="0"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           FontSize="10"
                                           FontFamily="{StaticResource RobotoRegular}" />

                                <TextBlock Grid.Row="3"
                                           Grid.Column="0"
                                           Text="Profit"
                                           HorizontalAlignment="Right"
                                           VerticalAlignment="Center"
                                           FontSize="10"
                                           FontFamily="{StaticResource RobotoBold}" />
                                <TextBlock x:Name="lblProfit"
                                           Grid.Row="3"
                                           Grid.Column="1"
                                           Text="0"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           FontSize="10"
                                           FontFamily="{StaticResource RobotoRegular}" />
                            </Grid>
                        </Grid>
                    </Grid>
                </BlockUIContainer>
            </FlowDocument>
        </FlowDocumentPageViewer>
    </Grid>
</Window>

总体而言,ListView中的项目是动态的。当列表长度超过Window的高度时,会出现问题,从而破坏报告的外观(如ellipse),并且与打印版本相同。

以下是代码隐藏:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        PrintDialog printDlg = new PrintDialog();
        var actualWidth = lstView.ActualWidth - 10;
        lstHeader1.Width = actualWidth / 6;
        lstHeader2.Width = actualWidth / 6;
        lstHeader3.Width = actualWidth / 6;
        lstHeader4.Width = actualWidth / 6;
        lstHeader5.Width = actualWidth / 6;
        lstHeader6.Width = actualWidth / 6;
        PopulateListView(ChosenDate);
        string businessName = "";
        string businessAddress = "";
        string businessTIN = "";
        string businessContact = "";
        string businessImage = "";
        using (SqlConnection conn = new SqlConnection(constr))
        {
            conn.Open();
            string query = "SELECT * FROM dbo.Company";
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                try
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                businessName = reader["Name"].ToString();
                                businessAddress = reader["Address"].ToString();
                                businessTIN = reader["TIN"].ToString();
                                businessContact = reader["Contact"].ToString();
                                businessImage = reader["ImagePath"].ToString();
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message + err.StackTrace);
                }
            }
        }
        BitmapImage img;
        if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + businessImage))
        {
            img = new BitmapImage();
            img.BeginInit();
            img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            img.CacheOption = BitmapCacheOption.OnLoad;
            img.UriSource = new Uri(AppDomain.CurrentDomain.BaseDirectory + businessImage);
            img.EndInit();
        }
        else
        {
            img = new BitmapImage(new Uri("pack://application:,,,/Resources/default_product_img.png"));
        }
        imgUser.ImageSource = img;
        lblCompanyName.Text = businessName;
        lblCompanyAddress.Text = businessAddress;
        lblCompanyTIN.Text = businessTIN;
        lblCompanyNumber.Text = businessContact;
        lblReportType.Text = ReportType;
        lblReportDate.Text = ChosenDate.ToString("MM/dd/yyyy");

        lblTotalSold.Text = TotalSold.ToString();
        lblTotalCost.Text = TotalCost.ToString("F2");
        lblTotalRetail.Text = TotalRetail.ToString("F2");
        lblProfit.Text = TotalProfit.ToString("F2");

        IDocumentPaginatorSource idpSource = flwDoc;
        var result = printDlg.ShowDialog();
        if (result == true)
        {
            printDlg.PrintDocument(idpSource.DocumentPaginator, "Document");
        }
    }

我认为Window中出现的用户界面仅用于视觉显示,而不是PrintDialog打印的内容,而是FlowDocument本身。如果不可能,我打算在代码隐藏中重新创建控件,以保持响应能力。所以我先问这里是否有解决方法。

0 个答案:

没有答案