10

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?

wonea
  • 4,783
  • 17
  • 86
  • 139
user38309
  • 2,268
  • 3
  • 21
  • 33
  • I am curious ... is 'see through the Winform chrome to whatever is beneath' actually the contents of the double buffering buffer? In our case, it was. Or, are you actually seeing what is underneath? You might be able to differentiate in a navigation scenario like mine. – cplotts Mar 07 '09 at 14:47
  • Another way to make a determination if it is the double buffering buffer is that the contents that you see show up ... are often mangled or messed up. – cplotts Mar 07 '09 at 14:48
  • You could try reporting the issue on connect rather than just posting on the forum. I've found them to be very responsive about stuff on Connect, especially if you attach code / repro steps etc. http://connect.microsoft.com/ – paulecoyote Sep 01 '09 at 14:43

5 Answers5

12

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 ...

cplotts
  • 13,941
  • 9
  • 55
  • 66
  • Thanks for the input, we ended up doing a similar and equally ugly hack – user38309 Mar 02 '09 at 22:33
  • Thanks summergoat. As you can see in my updated answer, I have submitted another WPF forum post. We'll see if anything comes from that. – cplotts Mar 03 '09 at 16:37
  • I've just received a collection of screenshots showing the same problem in a bug report from a customer. Interestingly, the screenshots are also of a Vista / Aero setup (as described in the WPF forum post), so it sounds like that might be a key factor. Just thought I'd share that, as it hasn't had a mention on this SO page yet. – Mal Ross Feb 03 '11 at 09:16
  • 1
    Correction: my customer's seen the problem on Windows 7, not Vista. – Mal Ross Feb 03 '11 at 16:42
  • I made it alternate between ++ and -- so the form dosent keep growing. Dirty but it works! – Marcom Jul 27 '11 at 14:25
  • FYI, we had to end up doing the `Visible = false/true` dance instead. Something weird was happening with our "dynamic" forms when playing with the `Width` (even with ++/-- combo). So, we set the `Visible = false` at design time or when we first add the `ElementHost`. And in the `HandleCreated` event, we then set `Visible = true`. Hope this helps. – Eric Liprandi Nov 01 '11 at 21:55
  • hi, I am having problem with `element_host` control when I am loading it on windows form. It doesn't refresh when I put `element_host.Width++` can you please help me out here. I want to user control to be refreshed when button is pressed. thank you – user123_456 May 31 '12 at 20:29
  • We are having the same problem. On some machines it is much more frequent than on others. Sometimes, the resulting screen is a mix of the background being obscured and the new content blended together. I believe that the "paint" is happening correctly, but the windows animation is somehow getting interrupted. The Windows animation that I'm seeing (on windows 7) is a crossfade from the background to the dialog contents, along with shifting the dialog contents. If I turn off "unnecessary animations" (in the windows Control Panel), then the dialog always works. To me, that's not a fix. – Dave Tillman Mar 15 '16 at 22:26
1

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

elementHost1.HostContainer.InvalidateVisual();
Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
CraigLee
  • 21
  • 3
1

I know this post is old, but in the later version of .net (i.e 3.5), you can try to use software rendering mode. This seems to fix a lot of problems when you host a WPF control in the ElementHost.

Ref: Software rendering mode - WPF

Community
  • 1
  • 1
dsum
  • 1,433
  • 1
  • 14
  • 29
  • Software rendering mode fixed this problem for me but led me into another more serious problem! http://stackoverflow.com/questions/17473857/what-causes-windows-to-hang-in-this-wpf-ribbon-application?noredirect=1#comment50273626_17473857 – Eduardo Wada Jun 30 '15 at 17:19
0

Maybe call Invalidate on the ElementHost?

casperOne
  • 73,706
  • 19
  • 184
  • 253
0

Have you tried to use reflection to see the code behind _elementHost.Width++?

Captain
  • 713
  • 1
  • 7
  • 20
  • I have used .NET Reflector to take a look at the code behind ElementHost, and actually there seems to be a lot of knobs to tinker with. Unfortunately, I've already spent too much time on this issue. However, the Width property is actually implemented on the base class. No help there. – cplotts Mar 07 '09 at 14:45