Partilhar via


Walkthrough: hospedando um controle de usuário Formulários do Windows usando o Criador WPF

Esta explicação passo a passo mostra como usar o WPF Designer for Visual Studio para o host umcontrole composto do Windows Formsem umaplicativodo WPF. Para obter mais informações sobre interoperabilidade do Windows Forms e WPF , consulte Migração e Interoperabilidade

Tasks explained in this walkthrough include:

  • Creating the project.

  • Creating the Windows Forms composite control.

  • Hosting the Windows Forms control in a WPF application.

ObservaçãoObservação

Caixas de diálogo e comandos de menu que você vê podem diferir das descritas na Help dependendo das suas configurações ativas ou edição. Para alterar as configurações, escolha Import and Export Settings sobre o Ferramentas menu. For more information, see Trabalhando com configurações.

Pré-requisitos

You need the following components to complete this walkthrough:

  • Visual Studio 2010.

Creating the Project

The first step is to create the project for the host application.

To create the project

  1. Crie um aplicativo WPF projeto chamado "demoapplication". For more information, see Como: Criar um novo projeto de aplicativo WPF.

    MainWindow. XAML é aberto no WPF Designer. 

  2. In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.

  3. Em Solution Explorer, adicione uma referência aoassemblyde Windows Forms, que é chamada de dll.

Creating the Windows Forms Composite Control

This procedure shows how to create a composite control which sizes itself to its content. The TableLayoutPanel control and the AutoSize property provide the control’s dynamic layout capability. For more information, see Controle TableLayoutPanel (Windows Forms).

To create the Windows Forms composite control

  1. Add a new Windows Forms Control Library project named "DemoControlLibrary" to the solution. For more information, see Como: Adicionar e remover itens de solução.

    UserControl1 opens in the Windows Forms Designer

  2. From the Toolbox, double-click the TableLayoutPanel control. A TableLayoutPanel control appears on the design surface.

  3. Select Remove Last Row from the TableLayoutPanel Tasks smart tag menu.

  4. In the Properties window, set the value of the Dock property to Fill.

  5. From the Toolbox, add a Label control to the first cell of the TableLayoutPanel control.

  6. In the Properties window, set the following properties for the Label control.

    Property

    Value

    AutoSize

    True

    Dock

    Fill

    Margin

    0,0,0,0

    Text

    Enter Date:

    TextAlign

    MiddleCenter

  7. From the Toolbox, add a MaskedTextBox control into the second cell of the TableLayoutPanel control.

  8. Click the smart tag glyph (Glifo de marca inteligente) on the MaskedTextBox control and select Set Mask from the MaskedTextBox Tasks menu.

    The Input Mask dialog box appears.

  9. Do de máscara Descrição coluna, selecione Data abreviada (ou outro predefinidos de descrição de máscara ) e clique no OK botão.

  10. Na parte superior a Propriedades janela, selecione o TableLayoutPanel controle na-suspensa lista.

  11. Set the value of the AutoSize property to true.

  12. At the top of the Properties window, select UserControl1.

  13. Set the value of the AutoSize property to true, and set the value of the Size property to 0,0.

    UserControl1 resizes to fit its contents.

  14. On the Build menu, select Build Solution to build the solution.

Hosting the Windows Forms Control in WPF

Você pode usar o WindowsFormsHost elemento para o host UserControl1 em umaplicativodo WPF.

To host the Windows Forms control in WPF

  1. In the DemoApplication project of Solution Explorer, add a reference to the DemoControlLibrary project. For more information, see Como: Adicionar ou remover referências no Visual Studio.

  2. Abrir MainWindow. XAML na WPF Designer.

  3. In XAML view, import the DemoControlLibrary namespace by inserting the following namespace mapping in the opening Window tag. For more information, see Como: Importar um namespace em XAML.

    xmlns:dc="clr-namespace:DemoControlLibrary;assembly=DemoControlLibrary"
    
  4. Após a Grid a abertura de marca, insira o código a seguir.

    <WindowsFormsHost HorizontalAlignment="Left" VerticalAlignment="Top">
        <dc:UserControl1/>
    </WindowsFormsHost>
    

    A region representing the hosted Windows Forms control appears in the designer.

  5. Executar a solução para teste o controle de usuário .

Próximas etapas

You can also host WPF controls in Windows Forms. For more information, see Usando os controles do WPF.

Consulte também

Tarefas

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

Referência

WindowsFormsHost

ElementHost

Outros recursos

Trabalhar com controles no criador de WPF

Migração e Interoperabilidade