打印期间未显示 WPF 验证红色边框装饰

WPF validation red border adornment not shown during printing

我正在尝试打印包含单个 TextBoxWindow。当显示在屏幕上时,红色验证错误边框正确显示。通过 PrintDialog 打印时,边框丢失。 window 定义为:

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、视图模型和错误实现。它指定单独的 属性、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 很好,但没有红色边框。我试过在 MainContent 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 Printing of an Adorner:

<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>