Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms WPF Interop - WPF content fails to paint

I have a WinForm that uses an ElementHost to display a WPF UserControl. Once every 50 times or so when the form loads the WPF content fails to paint. You can see through the WinForm chrome to whatever is beneath. Resizing the window gets the WPF content to show up.

Is this a known issue? Can anyone suggest a workaround?

like image 836
user38309 Avatar asked Jan 22 '09 21:01

user38309


2 Answers

We have fought these types of issues before. See this WPF forum post for more info on our particular flavor (I don't know if it is the same issue or not).

The only thing that we found to work was to change the size of the ElementHost.

_elementHost.Width++;

It's a complete hack, ugly, and I'm embarrassed to even post it. But nothing else ever worked for us. So, it is definitely a workaround. (Grin)

We tried Invalidate, Refresh and everything we could think of ... on the ElementHost. We also tried InvalidateMeasure, InvalidateArrange, and InvalidateVisual on the WPF hosted content. No luck.

If you find another way to fix your issue, I would love to hear about it.

Good luck, I know I have lost some hair on this one.

Update 1: I have submitted another WPF forum post on this. Maybe we can get a response from Microsoft. Sure seems like a bug to me.

Update 2: After I fixed the refresh issue with the above hack ... I still had another problem to solve that I thought worth mentioning here. That is: there was a definite delay until the screen refreshed. This made it seem like the user was navigating to another screen (it wasn't ... it was just the contents of the double buffering buffer). I ended up having to manually call System.Windows.Forms.Control.Refresh() on the Control that was hosting the ElementHost. In this way, even though the pause was still there ... at least the screen was blank ... and it didn't look like the user was navigating somewhere ...

like image 67
cplotts Avatar answered Sep 18 '22 14:09

cplotts


the following worked for me.
On the Form_Activated event, I added the following

elementHost1.HostContainer.InvalidateVisual();
like image 38
CraigLee Avatar answered Sep 18 '22 14:09

CraigLee