2016-04-22 3 views
0

UWPアプリケーションからテキストのページを印刷しようとしていますが、整列に問題があります。 TextBlockをグリッド内の中心に位置するように整列される方法テキストボックスの整列UWPから印刷する場合

<Grid Background="White"> 
    <TextBlock Text="Welcome Printed World!" FontSize="36" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
</Grid> 

お知らせ:まず第一に、私は次のXAMLを含むページを作成しました。

次に、さまざまな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); 
} 

、これは、それが印刷する方法である:テキストブロックを中心にされていないことを

enter image description here

お知らせ。私はこのように、国境でのTextBlockをラップする場合

しかし、:

<Grid Background="White"> 
    <Border HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <TextBlock Text="Welcome Printed World!" FontSize="36" Foreground="Black"/> 
    </Border> 
</Grid> 

そして、それは結構です。

enter image description here

使用した場合のTextBlockを中心にされていない理由を誰が見ることができます元のxaml?

+0

グリッドの中心に垂直方向の位置合わせと水平方向の位置合わせも行います。 – Archana

+0

これでうまくいきましたが、他の場所、たとえば右下に他のTextBlockを配置したい場合は問題があります。そのため、グリッドがページを埋める必要があると思います。私が見つけたのは、BorderThickessが0のTextBlockの代わりにTextBoxを使うことができるので、印刷されたページのテキストだけです。 –

答えて

1

Windows 8.1からUWPプロジェクトにアップグレードするときに同じ問題が発生しました。

UWP、予想通りプロパティは動作しませんTextBlock.VerticalAlignmentTextBlock.Horizo​​ntalAlignmentに印刷します。

あなたは実行要素の代わりに、TextBlock.Textプロパティを使用する場合は、彼らが期待どおりに動作するように見える:

<TextBlock FontSize="36" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center"> 
    <Run Text="Welcome Printed World!" /> 
</TextBlock> 

代わりにあなたが垂直方向の配置のためTextBlock.Marginを使用することができます水平整列の場合はTextBlock.TextAlignmentです。