WindowsFormsHost Winform pdfviewer control problem

老子叫甜甜 提交于 2019-12-08 13:23:52

问题


I have a wpf Usercontrol, inside i am using a Winforms pdfviewer to display pdf files. Also i have couple of Textboxes to enter document details. finally, A popup which display this user control. The problem is, when i try to type something in textboxes, ntn is happenning. when i right click on a textbox, i can see context menu with cut, copy and paste options. After googling little bit, i found something like below, Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop(), I placed this line in loaded event but this is not working. can any one faced simillar issue and have any solutions. Thanks. Rey


回答1:


I ran into this problem a while back. As I recall it was a bug having to do with the top level WPF message loop not playing nice with the WinForms message loop.

The solution I used was to change my outermost layer from a WPF Window to a WinForms Form. In other words, I replaced

new Window { Content = CreateContent(), Title = title }.Show();

with

new ElementHostForm(CreateContent(), title).Show();

by using a class like this:

class ElementHostForm : System.Windows.Forms.Form
{
  ElementHost _host;

  public WinFormsWindow(UIElement content, string title)
  {
    _host = new ElementHost { Child = content };
    Controls.Add(host);

    content.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    if(content.DesiredSize.Width > 100 && content.DesiredSize.Height > 100)
      ClientSize = _host.Size =
        new Size((int)content.DesiredSize.Width, (int)content.DesiredSize.Height));

    content.ClearValue(FrameworkElement.WidthProperty);
    content.ClearValue(FrameworkElement.HeightProperty);

    Title = title;
  }

  protected override void OnResize(EventArgs e)
  {
    if(!ClientSize.IsEmpty) _host.Size = ClientSize;
    base.OnResize(e);
  }
}

This worked around the bug by allowing WinForms to have the outermost message loop.

This change was very easy for me because I already had my top-level content in a separate UserControl (not a Window). If your top-level content is a Window you may need to refactor.



来源:https://stackoverflow.com/questions/2030306/windowsformshost-winform-pdfviewer-control-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!