我正在尝试打印包含单个TextBox
的Window
。 当显示在屏幕上时,红色验证错误边框正确显示。 当通过PrintDialog
打印时,边框丢失。 该窗口定义为:
public partial class ReportPage : INotifyDataErrorInfo
{
public ReportPage()
{
DataContext = this;
SomeText = "hi there";
InitializeComponent();
}
public string SomeText { get; set; }
public IEnumerable GetErrors(string propertyName)
{
return propertyName == "SomeText" ? new [] {"some error"} : null;
}
public bool HasErrors => true;
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
}
请注意它如何用作Window
、视图模型和错误实现。 它指定 lone 属性 SomeText 出错。 相应的 XAML 如下所示:
<Window x:Class="ReportPage"
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"
mc:Ignorable="d">
<Grid x:Name="MainContent" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBox x:Name="MyText" Text="{Binding SomeText}"/>
</Grid>
</Window>
要打印我使用的Window
的内容:
var printDialog = new PrintDialog { PrintTicket = { PageOrientation = PageOrientation.Portrait } };
printDialog.ShowDialog();
var caps = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
var rect = new Rect(new Point(caps.PageImageableArea.OriginWidth, caps.PageImageableArea.OriginHeight),
new Size(caps.PageImageableArea.ExtentWidth, caps.PageImageableArea.ExtentHeight));
var pageSize = rect.Size;
var origin = rect.TopLeft;
var fullSize = new Size(printDialog.PrintTicket.PageMediaSize.Width.Value,
printDialog.PrintTicket.PageMediaSize.Height.Value);
var page = new ReportPage
{
Margin = new Thickness(
Math.Max(72 - origin.X, 0),
Math.Max(48 - origin.Y, 0),
Math.Max(72 - (fullSize.Width - pageSize.Width - origin.X), 0),
Math.Max(96 - (fullSize.Height - pageSize.Height - origin.Y), 0))
};
page.MainContent.Measure(pageSize);
page.MainContent.Arrange(new Rect(origin, pageSize));
page.MainContent.UpdateLayout();
printDialog.PrintVisual(page.MainContent, "some description");
我看到TextBox
很好,但没有红色边框。 我尝试在主内容Grid
周围添加<AdornerDecorator>
,但无济于事。 在屏幕上显示Window
导致红色边框可见而打印时不会出现时会发生什么情况?
首先,如果不显示Window
,就不会渲染装饰品。 要解决此问题,Window
在打印之前,显示在屏幕外(Left
/Top
任意设置为 -10000,ShowInTaskbar
/ShowActivated
设置为 false
):
page.Show();
page.MainContent.Measure(pageSize);
page.MainContent.Arrange(new Rect(origin, pageSize));
page.MainContent.UpdateLayout();
printDialog.PrintVisual(page.MainContent, "some description");
其次,仅打印 MainContent 时没有装饰层,因此必须添加装饰层(请参阅 XPS 打印装饰器:
<Window x:Class="ReportPage"
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"
mc:Ignorable="d"
ShowInTaskbar="False"
Left="-10000"
Top="-10000"
ShowActivated="False">
<Window.Resources>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<AdornerDecorator>
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</AdornerDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ContentControl x:Name="MainContent" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBox x:Name="MyText" Text="{Binding SomeText}"/>
</Grid>
</Window>