14
votes

I am having a WinForms control, inside that I have a TableLayoutPanel which holds multiple ElementHosts and each ElementHost contains a WPF control.

Everything works fine except when the size of controls is bigger then window and ScrollBar is there; when I scroll down, the controls get rendered distorted, like this -

enter image description here

On maximizing the window or re-sizing it, controls render properly (reducing the size such that controls go out of visible area and then increase the size again to bring them back in visible area)

This doesn't happen with WinForms control in the same window just the WPF ones; any idea why this is happening and any solution for this?

3
Anyone, anything....let me know if any other detail can be of help in solving this issue!akjoshi
Note that on Windows 7 this problem does not occur when using a "classic" Windows theme. It seems to happen only when using the "Aero" theme.Dimitri C.
@DimitriC. Thanks for info, didn't noticed that in this case but yes I have come across some theme related issues in WPF/WinForms interoperability and they always reminds me of browser incompatibility issues and hacks used their :)akjoshi

3 Answers

15
votes
this.Loaded += delegate
{
    var source = PresentationSource.FromVisual(this);
    var hwndTarget = source.CompositionTarget as HwndTarget;

    if (hwndTarget != null)
    {
        hwndTarget.RenderMode = RenderMode.SoftwareOnly;
    }
};

Try using that in the wpf control you are hosting. This is a known rendering issue of the the wpf controls that are hosted in win forms. Changing the rendering mode to software only will solve the problem.

1
votes

I had a similar problem and solved forcing a refresh of the ElmenetHost in the scroll event of the TableLayoutPanel

0
votes

Ok, this is gonna sound like total B.S. but it worked for me: in the Load event of your form, resize the form.

public class MyForm : Form
{
   public MyForm()
   {
      Load += (o, e) => { Width -=1; Width +=1; };
   }
}

After the form has been resized, I could not force a display issue.