简体   繁体   English

从Form移植到WPF

[英]Porting from Form to WPF

I have piece of Code in Form that works: 我在表单中有一段有效的代码:

public class Form1 : System.Windows.Forms.Form
{
    private void Form1_Load(object sender, System.EventArgs e)
    {
       port.Parent = this;
    }
}

now I changed to WPF: 现在我改为WPF:

public partial class MainWindow : RibbonWindow
{
    private void Btn_Click(object sender, RoutedEventArgs e)
    {
       port.Parent = this;
    }
}

then it gives me: "Cannot implicitly convert type 'System.Windows.DependencyObject' to 'System.Windows.Forms.Control'" 然后它给了我: “不能隐式地将类型'System.Windows.DependencyObject'转换为'System.Windows.Forms.Control'”

What is the problem? 问题是什么?

You are using WPF and Windows forms in one application. 您在一个应用程序中使用WPF和Windows窗体。 You cannot use a Windows Forms window as a parent for a WPF window. 您不能将Windows窗体窗口用作WPF窗口的父窗口。 You should convert both Windows to WPF, or host your WPF elements in a Windows Forms ElementHost control, using a WinForms window. 您应该使用WinForms窗口将Windows转换为WPF,或者在Windows窗体ElementHost控件中托管WPF元素。

因为所有wpf控件都是DependencyObject的子类型,所以不能为父提供非DependencyObject实例,这就是问题所在。

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

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