我有一个wpf用户控件,里面我使用Winforms pdfviewer来显示pdf文件。此外,我还有几个文本框来输入文档详细信息。最后,显示此用户控件的弹出窗口。问题是,当我尝试在文本框中键入内容时,ntn正在发生。当我右击一个文本框时,我可以看到带有剪切、复制和粘贴选项的上下文菜单。在谷歌了一下之后,我找到了类似下面的东西,Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop(),我把这一行放在了loaded event中,但这不起作用。任何人都能面对类似的问题并有任何解决方案吗?谢谢。雷伊
发布于 2010-01-12 08:06:25
前段时间我遇到了这个问题。在我的记忆中,这是一个与顶层WPF消息循环有关的错误,不能很好地处理WinForms消息循环。
我使用的解决方案是将最外层从WPF窗口更改为WinForms表单。换句话说,我替换了
new Window { Content = CreateContent(), Title = title }.Show();使用
new ElementHostForm(CreateContent(), title).Show();通过使用如下所示的类:
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);
}
}这通过允许WinForms拥有最外层的消息循环解决了错误。
这个改变对我来说非常简单,因为我已经在一个单独的UserControl (而不是一个窗口)中拥有了我的顶级内容。如果你的顶层内容是一个窗口,你可能需要重构。
https://stackoverflow.com/questions/2030306
复制相似问题