Rendering issue with WPF controls inside ElementHost
Asked Answered
M

3

14

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?

Mistaken answered 8/8, 2012 at 7:36 Comment(4)
Anyone, anything....let me know if any other detail can be of help in solving this issue!Mistaken
Possible duplicate #2590448Aphyllous
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.Egalitarian
@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 :)Mistaken
L
15
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.

Leavings answered 28/9, 2012 at 8:59 Comment(6)
Thanks Valentin, Sounds interesting, will try this solution and hope it would work.Mistaken
Some info regarding performance impact is mentioned hereMistaken
Works fine for me. I have splitters and several ElementHosts, the fragmenting and black parts do no longer show after applying this method.Thessa
This worked on my machine but caused some others to crash (#17474357 and #7720127) I still couldn't find a better solutionShowcase
Does this mean that we will lose performance while using software rendering and more rely on CPU?Contract
I had a wpf control inside a winform elementhost that wouldn't draw on ONE of my dual screen setup (But worked fine on the other screen!?). This solved the problem.Sagittal
N
1

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

Nitz answered 16/8, 2012 at 9:21 Comment(2)
Thanks Mackho, will try this and see if it works. BTW did this had any impact on the performance?Mistaken
No I didn't notice any significant decrease of performance, but I have to say my form is pretty light so i can't guarantee you anything ;)Nitz
H
1

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.

Highbred answered 30/7, 2015 at 23:7 Comment(4)
That sounds really interesting, I will try to test this and see if it works in my application.Mistaken
I'd like to know why this got a downvote. It actually solved the problem for meHighbred
Not sure when/who downvoted it, but its too old for anyone to remember. I also don't remember if I ever tried that or not, but upvoting it as its a potential solution for sure.Mistaken
Actually, we still use this approach in some of our code. Worth to consider as an option.Youth

© 2022 - 2024 — McMap. All rights reserved.