简体   繁体   English

使用ElementHost中的WPF控件呈现问题

[英]Rendering issue with WPF controls inside ElementHost

I am having a WinForms control, inside that I have a TableLayoutPanel which holds multiple ElementHosts and each ElementHost contains a WPF control. 我有一个WinForms控件,里面有一个TableLayoutPanel ,它包含多个ElementHosts ,每个ElementHost包含一个WPF控件。

Everything works fine except when the size of controls is bigger then window and ScrollBar is there; 一切正常,除非控件的大小比窗口和ScrollBar更大; when I scroll down, the controls get rendered distorted, like this - 当我向下滚动时,控件变得扭曲,就像这样 -

在此输入图像描述

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; 在WPF窗口的同一窗口中,WinForms控件不会发生这种情况; any idea why this is happening and any solution for this? 任何想法为什么会发生这种情况以及任何解决方案?

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. 尝试在您托管的wpf控件中使用它。 This is a known rendering issue of the the wpf controls that are hosted in win forms. 这是win表单中托管的wpf控件的已知呈现问题。 Changing the rendering mode to software only will solve the problem. 仅将渲染模式更改为软件才能解决问题。

我有一个类似的问题,并解决了在TableLayoutPanel的滚动事件中强制刷新ElmenetHost

Ok, this is gonna sound like total BS but it worked for me: in the Load event of your form, resize the form. 好吧,这听起来像总BS,但它对我有用:在表单的Load事件中,调整表单大小。

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. 在调整表单大小后,我无法强制显示问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM