【发布时间】:2016-07-20 16:46:04
【问题描述】:
我正在尝试从 UWP 应用打印一页文本,但我遇到了对齐问题。首先,我创建了一个包含以下 xaml 的页面:
<Grid Background="White">
<TextBlock Text="Welcome Printed World!" FontSize="36" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
注意 TextBlock 如何在网格中居中对齐。
然后,我使用各种 PrintDocument 事件处理程序打印它;这是打印预览它的代码(简化为忽略边距和不可打印区域):
Page printPage;
private void OnPrintDocumentPaginate(object sender, PaginateEventArgs e)
{
// Construct an instance of the page to print, and tell Windows that there is only 1 page
this.printPage = new PrintPage();
printDocument.SetPreviewPageCount(1, PreviewPageCountType.Final);
}
private void OnPrintDocumentGetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
// Give Windows a reference to the page to print for preview
this.printDocument.SetPreviewPage(e.PageNumber, this.printPage);
}
这就是它的打印方式:
请注意,TextBlock 没有居中。
但是,如果我将 TextBlock 包装在边框中,如下所示:
<Grid Background="White">
<Border HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Welcome Printed World!" FontSize="36" Foreground="Black"/>
</Border>
</Grid>
那就好了:
任何人都可以看到为什么使用原始 xaml 时 TextBlock 不居中吗?
【问题讨论】:
-
将垂直对齐和水平对齐也设置为网格的中心。
-
这行得通,但是如果我希望其他文本块在其他位置对齐,例如在右下角,我会遇到问题,所以我认为网格需要填充页面。我发现我可以使用 TextBox 而不是 TextBlock,BorderThickess 为 0,所以它只是打印页面上的文本。