views:

30

answers:

1

It seems not all bindings are to evaluated when printing. For example, in the code below, only the first button has content = "100", other buttons have content = "0".

var doc =   new XpsDocument("test.xps",FileAccess.Write);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
var collator = writer.CreateVisualsCollator();
collator.BeginBatchWrite();
for (int i = 0; i < 3; i++)
{
    var button = new Button();
    button.SetBinding(ContentControl.ContentProperty,
        new Binding
        {
            RelativeSource = new RelativeSource(RelativeSourceMode.Self),
            Path = new PropertyPath("ActualWidth")
        });
    button.Measure(new Size(100, 100));
    button.Arrange(new Rect(0, 0, 100, 100));
    button.Width = 100;
    button.Height = 100;
    collator.Write(button);
}
collator.EndBatchWrite();
doc.Close();

Is there a workaround?

For example, is there a way to force the binding to evaluate?

+1  A: 

Have you tried making sure the dispatcher is idle before the call to collator.EndBatchWrite(). Something like:

Dispatcher.CurrentDispatcher.Invoke(
    new Action( delegate { } ), DispatcherPriority.ApplicationIdle, null );
aciemian