Passo a passo: hospedar um controle do Windows Forms no WPFWalkthrough: Hosting a Windows Forms Control in WPF

O WPFWPF fornece muitos controles com um amplo conjunto de recursos.WPFWPF provides many controls with a rich feature set. No entanto, às vezes, convém usar controles do Windows FormsWindows Forms em suas páginas WPFWPF.However, you may sometimes want to use Windows FormsWindows Forms controls on your WPFWPF pages. Por exemplo, você pode ter um investimento substancial em controles existentes do Windows FormsWindows Forms ou pode ter um controle do Windows FormsWindows Forms que fornece funcionalidade exclusiva.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.

Este passo a passo mostra como hospedar um Windows FormsWindows Forms System.Windows.Forms.MaskedTextBox control em um WPFWPF página por meio de 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 obter uma listagem de código completa das tarefas mostradas neste passo a passo, consulte hospedando um controle de formulários do Windows no WPF de exemplo.For a complete code listing of the tasks shown in this walkthrough, see Hosting a Windows Forms Control in WPF Sample.

Pré-requisitosPrerequisites

É necessário o Visual Studio para concluir este passo a passo.You need Visual Studio to complete this walkthrough.

Hospedando o controle dos Windows FormsHosting the Windows Forms Control

Para hospedar o controle MaskedTextBoxTo host the MaskedTextBox control

  1. Crie um projeto de aplicativo WPF chamado HostingWfInWpf.Create a WPF Application project named HostingWfInWpf.

  2. Adicione referências aos assemblies a seguir.Add references to the following assemblies.

    • WindowsFormsIntegrationWindowsFormsIntegration

    • System.Windows.FormsSystem.Windows.Forms

  3. Abra MainWindow.xaml no WPF DesignerWPF Designer.Open MainWindow.xaml in the WPF DesignerWPF Designer.

  4. Nome do Grid elemento grid1.Name the Grid element grid1.

    <Grid Name="grid1">
        
    </Grid>
    
  5. No modo de Design ou XAML, selecione o Window elemento.In Design view or XAML view, select the Window element.

  6. Na janela Propriedades, clique na guia Eventos.In the Properties window, click the Events tab.

  7. Clique duas vezes o Loaded eventos.Double-click the Loaded event.

  8. Insira o seguinte código para manipular o 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. Na parte superior do arquivo, adicione o seguinte Imports ou using instrução.At the top of the file, add the following Imports or using statement.

    using System.Windows.Forms;
    
    Imports System.Windows.Forms
    
  10. Pressione F5 para compilar e executar o aplicativo.Press F5 to build and run the application.

Consulte tambémSee also