开发者

How can I use WPF bindings while printing?

开发者 https://www.devze.com 2023-01-07 17:40 出处:网络
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\".

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);开发者_StackOverflow
}
collator.EndBatchWrite();
doc.Close();

Is there a workaround?

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


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 );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号