Wpf PrintDialog.PrintVisualElements是否为非活动VisualElements? 介绍
我有一个带有主窗口(MVVM)的应用程序,它由一个带有5个选项卡的Wpf PrintDialog.PrintVisualElements是否为非活动VisualElements? 介绍,wpf,printing,printdialog,Wpf,Printing,Printdialog,我有一个带有主窗口(MVVM)的应用程序,它由一个带有5个选项卡的TabViewer组成。每个选项卡包含一个滚动查看器、一个网格和网格中的几个用户控件(MVC) 我的主窗口的Xaml: <Window x:Class="CharacterSheetGenerator.View.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schema
TabViewer
组成。每个选项卡包含一个滚动查看器
、一个网格
和网格
中的几个用户控件
(MVC)
我的主窗口的Xaml:
<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>
<!-- [...] -->
现在,这方面存在一些问题:
- 自应用程序启动以来未加载的所有
都将是emtpy(我的第二个页面有一个数据网格。行仍然存在,但缺少所有值)UserControls
- 如果某个选项卡已打开,但不是当前打开的选项卡,则UserControls有其数据,但没有相应地调整大小(青色点线也有同样的问题)
- 是什么导致了这种可以说是奇怪的行为,我该如何解决它?
- 除了printDialog.PrintVisual()之外,还有其他方法可以打印我的页面吗?
DependencyProperties
likeDataContext本身是一个被标记的依赖属性,因此它继承了可视化树。因此,如果设置窗口的datacontext,则其中的usercontrol将具有相同的datacontext。就像usercontrol中的datagrid一样。datagrid中的行具有它们在itemssource中对应的任何项的datacontext。列是一种抽象。
//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;
}
}