Delen via


Overzicht: Een Besturingselement voor Windows Forms hosten in WPF

WPF biedt veel bedieningselementen met een uitgebreide set van functionaliteiten. Mogelijk wilt u echter soms Windows Forms-besturingselementen gebruiken op uw WPF-pagina's. U hebt bijvoorbeeld een aanzienlijke investering in bestaande Besturingselementen voor Windows Forms of u hebt een Besturingselement voor Windows Forms dat unieke functionaliteit biedt.

In dit overzicht ziet u hoe u een Besturingselement voor Windows Forms System.Windows.Forms.MaskedTextBox host op een WPF-pagina met behulp van code.

Zie Hosting a Windows Forms Control in WPF Samplevoor een volledig codeoverzicht van de taken in deze handleiding.

Vereiste voorwaarden

U hebt Visual Studio nodig om deze walkthrough te voltooien.

Het Besturingselement Windows Forms hosten

Om het MaskedTextBox-besturingselement te beheren

  1. Maak een WPF-toepassingsproject met de naam HostingWfInWpf.

  2. Voeg verwijzingen toe aan de volgende assemblies.

    • WindowsFormsIntegration

    • System.Windows.Forms

  3. Open MainWindow.xaml in de WPF Designer.

  4. Geef het Grid element een naam grid1.

    <Grid Name="grid1">
        
    </Grid>
    
  5. Selecteer in de ontwerpweergave of de XAML-weergave het element Window.

  6. Klik in het venster Eigenschappen op het tabblad Gebeurtenissen.

  7. Dubbelklik op de gebeurtenis Loaded.

  8. Voeg de volgende code in om de Loaded gebeurtenis af te handelen.

    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. Voeg bovenaan het bestand de volgende Imports of using instructie toe.

    using System.Windows.Forms;
    
    Imports System.Windows.Forms
    
  10. Druk op F5- om de toepassing te bouwen en uit te voeren.

Zie ook