.netwpfvisual-studio-2008documentviewerfixeddocument

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.


Solution

  • I know this had already been answered, but I think this answer is nicer because it doesn't require you to add a DocumentView.

    If there's a way to reference the resources by the key name and put them in the FixedDocument with XAML, please let me know. I can't seem to find a way to do that, but maybe it's possible.

    Use:

    var doc = System.Windows.Application.LoadComponent(new Uri("/FixedDocumentExample.xaml", UriKind.Relative)) as FixedDocument;
    doc.AddPages();
    

    Extension Method:

    using System.Collections;
    using System.Windows.Documents;
    
    public static class FixedDocumentExtended {
        public static void AddPages(this FixedDocument fixedDocument) {
            var enumerator = fixedDocument.Resources.GetEnumerator();
            while (enumerator.MoveNext()) {
                var pageContent = ((DictionaryEntry)enumerator.Current).Value as PageContent;
                if (pageContent != null) {
                    fixedDocument.Pages.Add(pageContent);
                }
            }
        }
    }
    

    XAML:

    <FixedDocument
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <FixedDocument.Resources>
            <PageContent x:Key="page1">
                <FixedPage Width="793.76" Height="1122.56">
                    <TextBlock Margin="50" Text="Page 1"/>
                </FixedPage>
            </PageContent>
            <PageContent x:Key="page2">
                <FixedPage Width="793.76" Height="1122.56">
                    <TextBlock Margin="50" Text="Page 2"/>
                </FixedPage>
            </PageContent>
        </FixedDocument.Resources>
    </FixedDocument>