用于非活动VisualElement的PrintDialog.PrintVisual?

时间:2019-06-04 17:12:31

标签: wpf printing printdialog

简介

我有一个带有MainWindow(MVVM)的应用程序,该应用程序由带有5个标签的TabViewer组成。每个标签都包含ScrollViewerGridUserControls中的几个Grid(MVC)。

我的MainWindow的Xml:

<Window x:Class="CharacterSheetGenerator.View.MainWindow"
    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:ctrl="clr-namespace:CharacterSheetGenerator.Control"
    xmlns:help="clr-namespace:CharacterSheetGenerator.Helpers"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    mc:Ignorable="d"
    Title="MainWindow" Height="1600" Width="1320" Loaded="Window_Loaded">
<Grid Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top">
        <Button Name="SaveButton" Command="{Binding LoadCommand}" Width="40" Height="30">
            <Image Source="/images/LoadButtonPicture.png"/>
        </Button>
        <Button Name="LoadButton" Command="{Binding SaveCommand}" Width="40" Height="30">
            <Image Source="/images/SaveButtonpicture.png" />
        </Button>
        <!-- Print Button currently in xaml.cs of my MainWindow -->
        <Button Name="Print Button" Click="Button3_Click" Width="40" Height="30">
            <Image Source="/images/PrintButtonPicture.png"/>
        </Button>
    </StackPanel>
    <TabControl Grid.Row="1">
        <TabItem Width="75" Height="20" Header="Übersicht">
            <ScrollViewer HorizontalScrollBarVisibility="Disabled" >
                <i:Interaction.Behaviors>
                    <help:BubbleScrollEvent />
                </i:Interaction.Behaviors>
                <!-- Grid I'd like to print -->
                <Grid Name="Übersicht">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="30"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="180"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Horizontal" Grid.Row="1">
                    <StackPanel>
                        <TextBlock FontSize="34" FontWeight="Bold" Text="Heldendokument"/>
                        <TextBlock FontSize="20" Text="Persönliche Daten" TextAlignment="Center"/>
                    </StackPanel>
                        <ctrl:CharacterAttributes VerticalAlignment="Top" AttributeList="{Binding Attributes, Mode=TwoWay}"/>
                </StackPanel>

                            <Grid Grid.Row="2">
                                <StackPanel>
                                <ctrl:CharacterOverview VerticalAlignment="Top" CharacterInformation="{Binding CharacterInformation}" StatusValues="{Binding StatusValues, Mode=TwoWay}" Traits="{Binding Traits, Mode=TwoWay}" Expirience="{Binding Expirience}" TraitClickCommand="{Binding OpenTraitViewCommand}"/>
                                </StackPanel>
                            </Grid>
                        </Grid>

            </Grid>
            </ScrollViewer>
        </TabItem>
        <TabItem Width="75" Height="20" Header="Fertigkeiten">
            <ScrollViewer HorizontalScrollBarVisibility="Disabled">
                <i:Interaction.Behaviors>
                    <help:BubbleScrollEvent />
                </i:Interaction.Behaviors>
                    <!-- Second Grid I'd like to print -->
                <Grid Name="Fertigkeiten">
                    <!-- [...] -->
        </TabItem>
        <!-- [...] -->

第一个标签页的示例:

First Tab "Übersicht" example view

我已经用几个红色边框标记了我的每个UserControl。

问题

如果要简单地使用PrintDialog.PrintVisual(Übersicht, "Printing Übersicht"),我已经实现了code from this answer并对其进行了修改,以便打印所有内容,而不只是打印青色的点线代表在DinA4页面上将被打印的区域。一个标签页,但我遇到了一些问题。

修改后的代码:

   //I tried to create an array and iterate through it
     System.Windows.FrameworkElement[] elements = { Übersicht as System.Windows.FrameworkElement, Fertigkeiten as System.Windows.FrameworkElement, Kampf as System.Windows.FrameworkElement,
                                                       Zauber as System.Windows.FrameworkElement, Inventar as System.Windows.FrameworkElement,};

        PrintDialog printDialog = new PrintDialog();
        if (printDialog.ShowDialog() == true)
        {

            foreach (FrameworkElement e in elements)
            {

                //store original scale
                Transform originalScale = e.LayoutTransform;
                //get selected printer capabilities
                System.Printing.PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);


                //get scale of the print wrt to screen of WPF visual
                double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / 1281, capabilities.PageImageableArea.ExtentHeight /
                               1800); // I had to change ActualWidth and Height to static Values, as Grids in Tabs, which haven't been opened at least once since the start of the application had no actual size

                //Transform the Visual to scale
                e.LayoutTransform = new ScaleTransform(scale, scale);

                //get the size of the printer page
                System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

                //update the layout of the visual to the printer page size.
                e.Measure(sz);
                e.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

                //now print the visual to printer to fit on the one page.
                printDialog.PrintVisual(e, "My Print");

                //apply the original transform.
                e.LayoutTransform = originalScale;

            }

        }

现在有一些问题:

  • 自应用程序启动以来尚未加载的所有UserControls将为空(我的第二页有一个DataGrid。行仍然存在,但所有值都丢失了) p>

  • 如果已打开选项卡,但不是当前打开的选项卡,则UserControl会保留其数据,但不会相应地调整大小(青色点线存在相同问题)

让我为您展示我第二个标签页的示例:(已扫描的DinA4页面)

第一个问题:(您可以看到带有彩色框的UserControl仍然有效,因为它已经在第一页上加载了

enter image description here

第二期:

enter image description here

仍然缺少一些代码吗?转到我的GitHub并克隆项目以自己尝试。

问题:

  • 是什么导致这种奇怪的行为,我该如何解决?
  • 除了printDialog.PrintVisual()还能打印我的页面吗?

0 个答案:

没有答案