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

时间:2021-11-17 15:53:38

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; when I scroll down, the controls get rendered distorted, like this -

一切正常,除非控件的大小比窗口和ScrollBar更大;当我向下滚动时,控件变得扭曲,就像这样 -

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

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?

在WPF窗口的同一窗口中,WinForms控件不会发生这种情况;知道为什么会这样,有什么解决方案吗?

3 个解决方案

#1


13  

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.

尝试在您托管的wpf控件中使用它。这是win表单中托管的wpf控件的已知呈现问题。仅将渲染模式更改为软件才能解决问题。

#2


1  

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

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

#3


0  

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

好吧,这听起来像总B.S.但它对我有用:在表单的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.

在调整表单大小后,我无法强制显示问题。

#1


13  

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.

尝试在您托管的wpf控件中使用它。这是win表单中托管的wpf控件的已知呈现问题。仅将渲染模式更改为软件才能解决问题。

#2


1  

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

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

#3


0  

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

好吧,这听起来像总B.S.但它对我有用:在表单的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.

在调整表单大小后,我无法强制显示问题。