Generating XPS Files with Catel

Mar 27, 2012 at 11:23 PM
Edited Mar 27, 2012 at 11:24 PM

As I understand it, nested views work by settings the DataContext of a UserControl to a Model, and the UserControl will switch its DataContext to a generated ViewModel that is constructed from that Model. This appears to be working fairly well in my program. However, I am trying to create XPS files - visual representations of some of the UserControls in my program - and the DataContext never changes from the Model I originally bind it to. Because it's binding to the Model and not a dynamically generated ViewModel, some of the visuals are missing and are therefore not printed in the .xps file. Is there a way to force the automatic generation of the ViewModel? Thanks in advance for any help you can provide.

Example Code here:

            FixedDocument doc = new FixedDocument();
            doc.DocumentPaginator.PageSize = new Size(96 * 11, 96 * 8.5);

            foreach (CLPPage page in notebook.Pages)
                PageContent pageContent = new PageContent();
                FixedPage fixedPage = new FixedPage();

                CLPPagePreviewView currentPage = new CLPPagePreviewView();

                currentPage.DataContext = page;


                //Rotate the page
                RotateTransform rotate = new RotateTransform(90.0);
                TranslateTransform translate = new TranslateTransform(816 + 2, -2);
                TransformGroup transform = new TransformGroup();
                currentPage.RenderTransform = transform;


            //Save the document
            string filename = notebook.NotebookName + ".xps";
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Notebooks - XPS\" + filename;
            if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Notebooks - XPS\"))
                Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Notebooks - XPS\");
            if (File.Exists(path))

            XpsDocument xpsd = new XpsDocument(path, FileAccess.ReadWrite);
            XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
Mar 28, 2012 at 7:50 AM

This is a very interesting question. The model is transformed into a view model as soon as the Loaded event fires, but this might not be working when not actually rendering the control (although it is rendered on the page). In 3.1, we made some changes to the way the model is transformed into a view model, and it now happens as soon as any datacontext is known (thus no longer delayed to the Loaded event), you might want to build that version and try it.

Mar 28, 2012 at 10:05 AM

Thanks for the reply. I built 3.1 and put that in, unfortunately the DataContext is still set to the Model and never changes to a ViewModel. Any other suggestions?

Mar 28, 2012 at 10:06 AM

Can you send me a small repro? Then I can try to fix it.

Mar 28, 2012 at 10:26 AM

I sent you a message on codeplex with the information. Thanks again for the help.