WPF VS2013 - 添加新窗口作为主窗口的子项目

时间:2015-04-05 15:32:32

标签: wpf window children mdi

我正试图在我的C#应用​​程序中从WinForms切换到WPF。我的WinForms应用程序是MDI应用程序。我需要在WPF中使它成为MDI。如何将新窗口锁定到我的应用程序主窗口或该窗口上的画布?我的子窗口无法进入应用程序主窗口。必须在主窗口的菜单项的点击事件上显示该子窗口。

1 个答案:

答案 0 :(得分:-1)

在您的情况下,最佳解决方案是使用System.Windows.Forms.Integration.ElementHost控件。此控制允许您放置任何您喜欢的WPF控件。跟着这些步骤: 1)在Windows窗体项目中创建子窗体 2)在此表单中添加System.Windows.Forms.Integration.ElementHost控件 3)在后面的代码做这样的事情:

wpfElementHost.Child = new System.Windows.Controls.TextBlock()
        {
            Text = "Hello from WPF world.",
            FontSize = 25,
            FontStyle = System.Windows.FontStyles.Italic,
            FontWeight = System.Windows.FontWeights.Bold,
            TextAlignment = System.Windows.TextAlignment.Center
        };

注意我使用了完全限定名称,因为Windows Forms和WPF有许多共同之处。在我的例子中,wpfElementHost是System.Windows.Forms.Integration.ElementHost。您可以托管任何控件,例如只需创建自定义控件或用户控件作为类库并在Windows窗体应用程序中使用它们。 希望这有帮助。如果我正确回答了你的问题,请告诉我。