Archive for September, 2008

In memory FlowDocument to XPS with working DataBinding Comments Off

It costs me some time and nerves to figure this out, but finally ive got this working.


var flowDocument = (FlowDocument)Application.LoadComponent( new Uri( @"MyFlowDocument.xaml", UriKind.Relative ) );

doc.DataContext = this;

var package = Package.Open( new MemoryStream(), FileMode.Create, FileAccess.ReadWrite );
var packUri = new Uri( "pack://temp.xps" );
PackageStore.RemovePackage( packUri );
PackageStore.AddPackage( packUri, package );

var xpsDocument = new XpsDocument( package, CompressionOption.SuperFast, packUri.ToString() );

var paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;

new XpsSerializationManager( new XpsPackagingPolicy( xpsDocument ), false )
.SaveAsXaml( paginator );

Viewer.Document = xpsDocument.GetFixedDocumentSequence();</code><strong></strong>

The key part here to get the binding to work is this call:


Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.SystemIdle,
new DispatcherOperationCallback(arg => null ), null);

Ive got this tip from the blog of Mark Nijhof. I dont know why this is need, but i assume that it needs one Dispatcher run to expand the DataBinding.