Tutorial: Hospedar un control de formularios Windows Forms en WPFWalkthrough: Hosting a Windows Forms Control in WPF

WPFWPF proporciona numerosos controles con un conjunto de características enriquecidas.provides many controls with a rich feature set. Sin embargo, es posible que a veces desea utilizar Windows FormsWindows Forms a los controles de su WPFWPF páginas.However, you may sometimes want to use Windows FormsWindows Forms controls on your WPFWPF pages. Por ejemplo, puede tener una inversión sustancial en existente Windows FormsWindows Forms controles, o bien puede tener un Windows FormsWindows Forms control que ofrece una funcionalidad única.For example, you may have a substantial investment in existing Windows FormsWindows Forms controls, or you may have a Windows FormsWindows Forms control that provides unique functionality.

En este tutorial se muestra cómo hospedar un Windows FormsWindows Forms System.Windows.Forms.MaskedTextBox control en un WPFWPF página mediante código.This walkthrough shows you how to host a Windows FormsWindows Forms System.Windows.Forms.MaskedTextBox control on a WPFWPF page by using code.

Para obtener una lista de código completo de las tareas mostradas en este tutorial, vea hospedar un Control de Windows Forms en WPF Sample.For a complete code listing of the tasks shown in this walkthrough, see Hosting a Windows Forms Control in WPF Sample.

Requisitos previosPrerequisites

Necesita Visual Studio para completar este tutorial.You need Visual Studio to complete this walkthrough.

Hospedar el control de Windows FormsHosting the Windows Forms Control

Para hospedar el control MaskedTextBoxTo host the MaskedTextBox control

  1. Cree un proyecto de aplicación WPF denominado HostingWfInWpf.Create a WPF Application project named HostingWfInWpf.

  2. Agregue referencias a los ensamblados siguientes.Add references to the following assemblies.

    • WindowsFormsIntegrationWindowsFormsIntegration

    • System.Windows.FormsSystem.Windows.Forms

  3. Abra MainWindow.xaml en el WPF DesignerWPF Designer.Open MainWindow.xaml in the WPF DesignerWPF Designer.

  4. Nombre de la Grid elemento grid1.Name the Grid element grid1.

    <Grid Name="grid1">
        
    </Grid>
    
  5. En la vista Diseño o la vista XAML, seleccione el Window elemento.In Design view or XAML view, select the Window element.

  6. En la ventana Propiedades, haga clic en el eventos ficha.In the Properties window, click the Events tab.

  7. Haga doble clic en el Loaded eventos.Double-click the Loaded event.

  8. Inserte el código siguiente para controlar el Loaded eventos.Insert the following code to handle the Loaded event.

    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);
    }
    
    Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Create the interop host control.
        Dim host As New System.Windows.Forms.Integration.WindowsFormsHost()
    
        ' Create the MaskedTextBox control.
        Dim mtbDate As 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.
        Me.grid1.Children.Add(host)
    
    End Sub
    
  9. En la parte superior del archivo, agregue el siguiente Imports o using instrucción.At the top of the file, add the following Imports or using statement.

    using System.Windows.Forms;
    
    Imports System.Windows.Forms
    
  10. Presione F5 para compilar y ejecutar la aplicación.Press F5 to build and run the application.

Vea tambiénSee also