在打印过程中未显示WPF验证红色边框装饰

时间:2019-01-10 23:09:05

标签: wpf validation printing adorner

我正在尝试打印包含单个Window的{​​{1}}。当在屏幕上显示时,正确显示红色的验证错误边框。通过TextBox打印时,缺少边框。该窗口定义为:

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; } ,视图模型和错误实现。它指定lone属性SomeText错误。相应的XAML如下:

Window

要打印<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"); 很好,但没有红色边框。我尝试在MainContent TextBox周围添加<AdornerDecorator>无济于事。在屏幕上显示Grid导致红色边框可见而不是打印时会发生什么?

1 个答案:

答案 0 :(得分:0)

首先,在不显示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>