Thursday 3 March 2011

Refresh WPF controls just like Winforms

Remember the bad old days with forcing repaints on your Winforms applications? I was forced back into that era today. I was updating the source of an Image on a WPF window, and then taking a snapshot of the window immediately after. What led to a great deal of hair loss was that the image hadn't actually appeared by the time I was taking the snapshot, and thus it was like taking a photo of a vampire - it simply wasn't there, even though the moment I could see the window it actually was.

Instead of going down the lengthy route of delaying the snapshot and then nested events or callbacks, I went to Google, and more specifically, my dear friend Stack Overflow which eventually led me to here.

this.Dispatcher.Invoke(DispatcherPriority.Render, _emptyDelegate);

That _emptyDelegate:

private static Action _emptyDelegate = delegate() { };

Genius. Continued hair loss averted. Or rather, delayed until the next issue...

No comments:

Post a Comment