Procédure pas à pas : hébergement d'un contrôle Windows Forms dans Windows Presentation Foundation

Mise à jour : novembre 2007

WPF fournit de nombreux contrôles dotés d'un ensemble de fonctionnalités riche. Toutefois, vous pouvez parfois souhaiter utiliser des contrôles Windows Forms sur vos pages WPF. Par exemple, vous pouvez avoir un investissement substantiel dans les contrôles Windows Forms existants, ou vous pouvez avoir un contrôle Windows Forms qui fournit des fonctionnalités uniques.

Cette procédure pas à pas vous indique comment héberger un contrôle Windows FormsSystem.Windows.Forms.MaskedTextBox sur une page WPF en utilisant du code.

Pour une liste de code complète des tâches affichées dans cette procédure pas à pas, consultez Hébergement d'un contrôle Windows Forms dans Windows Presentation Foundation, exemple.

Remarque Les boîtes de dialogue et les commandes de menu qui s'affichent peuvent être différentes de celles qui sont décrites dans l'aide, en fonction de vos paramètres actifs ou de l'édition utilisée. Pour modifier vos paramètres, choisissez Importation et exportation de paramètres dans le menu Outils. Pour plus d'informations, consultez Paramètres Visual Studio.

Composants requis

Les composants suivants sont nécessaires pour exécuter cette procédure pas à pas :

  • Visual Studio 2008.

Hébergement du contrôle Windows Forms

Pour héberger le contrôle MaskedTextBox

  1. Créez un projet Application WPF nommé HostingWfInWpf.

  2. Dans l'Explorateur de solutions, ajoutez une référence à l'assembly WindowsFormsIntegration nommé WindowsFormsIntegration.dll.

  3. Dans l'Explorateur de solutions, ajoutez une référence à l'assembly Windows Forms nommé System.Windows.Forms.dll.

  4. Ouvrez Window1.xaml dans le Concepteur WPF.

  5. Remplacez le code XAML généré automatiquement dans Window1.xaml par le code XAML suivant.

    <Window x:Class="Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="HostingWfInWpf" Height="300" Width="300"
        Loaded="WindowLoaded"
        >
      <Grid Name="grid1">
    
        </Grid>
    </Window>
    
    <Window x:Class="HostingWfInWpf.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="HostingWfInWpf"
        Loaded="WindowLoaded"
        >
        <Grid Name="grid1">
    
        </Grid>
    </Window>
    
  6. Dans l'éditeur de code, ouvrez Window1.xaml.cs.

  7. Remplacez le code dans Window1.xaml.cs par le code suivant.

    Imports System
    Imports System.Windows
    Imports System.Windows.Controls
    Imports System.Windows.Data
    Imports System.Windows.Documents
    Imports System.Windows.Media
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Shapes
    
    Imports System.Windows.Forms
    
    ' Interaction logic for Window1.xaml
    Partial Public Class Window1
        Inherits Window
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub WindowLoaded(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 'WindowLoaded
    
    End Class
    
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Media;
    using System.Windows.Shapes;
    
    using System.Windows.Forms;
    
    namespace HostingWfInWpf
    {   
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            private void WindowLoaded(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);
            }
        }
    }
    

Voir aussi

Tâches

Procédure pas à pas : hébergement d'un contrôle Windows Forms dans Windows Presentation Foundation avec XAML

Hébergement d'un contrôle Windows Forms dans Windows Presentation Foundation, exemple

Concepts

Procédure pas à pas : hébergement d'un contrôle composite Windows Forms dans Windows Presentation Foundation

Procédure pas à pas : hébergement d'un contrôle Windows Presentation Foundation dans les Windows Forms

Contrôles Windows Forms et contrôles WPF équivalents

Référence

ElementHost

WindowsFormsHost

Autres ressources

Concepteur WPF

Rubriques Comment relatives à la migration et à l'interopérabilité