【问题标题】:Inserting a winform control into WPF [closed]将 winform 控件插入 WPF [关闭]
【发布时间】:2013-09-09 20:56:49
【问题描述】:

我们有一个在 Windows 窗体中创建的旧库。我们想在现有的 WPF 应用程序中使用这个库。根据this link 是可能的。有没有人尝试过这样做?我们需要注意哪些注意事项?有什么建议吗?

谢谢!

【问题讨论】:

标签: c# wpf winforms


【解决方案1】:

关于这个主题有很多可用的资源。以下是从简单的 Google 搜索返回的一些

我将解释第一个链接,因为它相当简单且易于管理。它实际上没有多少代码。

首先您需要添加对以下程序集的引用:

  • WindowsFormsIntegration
  • System.Windows.Forms

然后创建一个网格(在本例中名为 Grid1)。

这是代码隐藏:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{
    // Create the interop host control.
    System.Windows.Forms.Integration.WindowsFormsHost host =
        new System.Windows.Forms.Integration.WindowsFormsHost();

    // Create the MaskedTextBox control.
    MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");

    // Assign the MaskedTextBox control as the host control's child.
    host.Child = mtbDate;

    // Add the interop host control to the Grid 
    // control's collection of child controls. 
    this.grid1.Children.Add(host);
}

您所做的只是实例化WindowsFormsHost,然后添加一个名为mtbDate 的子控件。实例化您自己的控件并使用相同的方法添加它。然后像往常在 Win Forms 中一样操作它。

然后在类的顶部添加一个 using:

using System.Windows.Forms;

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    相关资源
    最近更新 更多