Search code examples
c#wpfxamllayoutdocumentviewer

How can I center my page in the document viewer?


I'm using a document to show a user control. Some people from here helped me: How can I put an user control inside a document viewer?

But the user control appers in the corner, and I'd like to print it, but a little bit more central.


Solution

  • Repeating my updated answer from the other question..

    You can place the UserControl in a Grid which binds its Width/Height to the FixedPage ActualWidth/ActualHeight to achieve centering

    <DocumentViewer>
        <FixedDocument>
            <PageContent>
                <FixedPage>
                    <Grid Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type FixedPage}},
                                          Path=ActualWidth}"
                          Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type FixedPage}},
                                           Path=ActualHeight}">
                        <local:MyUserControl HorizontalAlignment="Center"
                                             VerticalAlignment="Center"/>
                    </Grid>
                </FixedPage>
            </PageContent>
        </FixedDocument>
    </DocumentViewer>