Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: FixedDocument in Visual Studio 2008 Designer

It's a well-known bug that Visual Studio shows an error when you try to construct a FixedDocument in XAML. For example, the following snippet

<DocumentViewer>
    <FixedDocument>
        <PageContent>
            <FixedPage Width="21.0cm" Height="29.7cm">
                <TextBlock>Hello World!</TextBlock>
            </FixedPage>
        </PageContent>
    </FixedDocument>
</DocumentViewer>

compiles and runs perfectly fine, but Visual Studio shows an error in the error list (Property 'Pages' does not support values of type 'PageContent'.) This is quite annoying.

I'm looking for a solution that allows me to construct my documents in a XAML file in Visual Studio without getting that error message. I've found a workaround, which I'd like to share below as an answer, but I'm curious if there's a better (more elegant) solution around.

like image 539
Heinzi Avatar asked Jan 25 '10 15:01

Heinzi


People also ask

How do I view XAML design in Visual Studio?

To open the XAML Designer, right-click a XAML file in Solution Explorer and choose View Designer. to switch which window appears on top: either the artboard or the XAML editor.

What is WPF designer?

Windows Presentation Foundation is a UI framework that creates desktop client applications. The WPF development platform supports a broad set of application development features, including an application model, resources, controls, graphics, layout, data binding, documents, and security.

What markup language is used in WPF?

Most WPF apps consist of both XAML markup and code-behind. Within a project, the XAML is written as a . xaml file, and a CLR language such as Microsoft Visual Basic or C# is used to write a code-behind file.


1 Answers

As a workaround, I put the DocumentViewer as well as the page into a grid:

<Grid>
    <FixedPage Width="21.0cm" Height="29.7cm" x:Name="uiPage1">
        <TextBlock>Hello World!</TextBlock>
    </FixedPage>
    <DocumentViewer>
        <FixedDocument x:Name="uiReport">
        </FixedDocument>
    </DocumentViewer>
</Grid>

Then I attach the page to the DocumentViewer in the Loaded event of the window:

VB example:

DirectCast(Me.uiPage1.Parent, Grid).Children.Remove(Me.uiPage1)
Dim content As New PageContent()
DirectCast(content, IAddChild).AddChild(Me.uiPage1)
Me.uiReport.Pages.Add(content)

C# example:

((Grid)uiPage1.Parent).Children.Remove(uiPage1);
var content = new PageContent();
((IAddChild)content).AddChild(uiPage1);
uiReport.Pages.Add(content);
like image 102
Heinzi Avatar answered Sep 22 '22 05:09

Heinzi