WindowsFormsHost Winform pdfviewer control problem

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

Answers


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.


Need Your Help

TortoiseSVN Commit failed: MERGE failed

svn github merge tortoisesvn tortoisemerge

I'm having troubles to commit any change after merging on pull-request from github respository.