Partilhar via


Demonstra Passo a passo: Hospedando um controle Windows Forms no WPF

Atualização: August 2010

WPF provides many controls with a rich feature set. However, you may sometimes want to use Windows Forms controls on your WPF pages. For example, you may have a substantial investment in existing Windows Forms controls, or you may have a Windows Forms control that provides unique functionality.

Esta explicação passo a passo mostra como hospedar um Windows Forms System.Windows.Forms.MaskedTextBox de controle em um WPF página usando código.

Para obter uma listagem de código completo das tarefas mostradas neste passo a passo, consulte que hospeda um controle Windows Forms, WPF de exemplo.

Pré-requisitos

You need the following components to complete this walkthrough:

  • Visual Studio 2010.

Hosting the Windows Forms Control

To host the MaskedTextBox control

  1. Create a WPF Application project named HostingWfInWpf.

  2. Add references to the following assemblies.

    • WindowsFormsIntegration

    • System.Windows.Forms

  3. Abrir MainWindow. XAML na WPF Designer.

  4. Nome do Grid elemento grid1.

    <Grid Name="grid1">
    
    </Grid>
    
  5. No modo de exibição de Design ou XAML, selecione o Window elemento.

  6. Na janela Propriedades, clique na eventos guia.

  7. Clique duas vezes o Loaded de evento.

  8. Insert the following code to handle the Loaded event.

    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
    
    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);
    }
    
  9. Na parte superior do arquivo, adicione o seguinte Imports ou using instrução.

    Imports System.Windows.Forms
    
    using System.Windows.Forms;
    
  10. Pressione F5 para criar e executar o aplicativo.

Consulte também

Tarefas

Demonstra Passo a passo: Hospedando um controle Windows Forms no WPF usando XAML

Referência

ElementHost

WindowsFormsHost

Conceitos

Demonstra Passo a passo: Hospedando um controle Windows Forms composto no WPF

Demonstra Passo a passo: Hospedando um controle composto do WPF no Windows Forms

Controles de Formulários do Windows e Controles WPF Equivalentes

Outros recursos

WPF Designer

Hospedando um controle Windows Forms, WPF de exemplo

Histórico de alterações

Date

History

Motivo

August 2010

Atualizado para 2010 de Visual Studio.

Comentários do cliente.