Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Aggiornamento: novembre 2007
WPF fornisce molti controlli con una vasta serie di funzionalità. Talvolta si potrebbero tuttavia utilizzare i controlli Windows Form nelle pagine WPF. È ad esempio possibile avere un investimento sostanziale in controlli Windows Form esistenti o è possibile disporre di un controllo Windows Form che fornisce funzionalità univoche.
In questa procedura dettagliata viene illustrato come eseguire l'hosting di un controllo System.Windows.Forms.MaskedTextBoxWindows Form in una pagina WPF tramite codice.
Per un elenco di codice completo delle attività illustrate in questa procedura dettagliata, vedere Esempio di hosting di controlli Windows Form in Windows Presentation Foundation.
Nota È possibile che le finestre di dialogo e i comandi di menu visualizzati siano diversi da quelli descritti nella Guida, a seconda delle impostazioni attive o dell'edizione del programma. Per modificare le impostazioni, scegliere Importa/Esporta impostazioni dal menu Strumenti. Per ulteriori informazioni, vedere Impostazioni di Visual Studio.
Prerequisiti
Per completare questa procedura dettagliata, è necessario disporre dei seguenti componenti:
- Visual Studio 2008.
Hosting di un controllo Windows Form
Per ospitare il controllo MaskedTextBox
Creare un progetto di applicazione WPF denominato HostingWfInWpf.
In Esplora soluzioni aggiungere un riferimento all'assembly WindowsFormsIntegration, denominato WindowsFormsIntegration.dll.
In Esplora soluzioni aggiungere un riferimento all'assembly Windows Form, denominato System.Windows.Forms.dll.
Aprire Window1.xaml in WPF Designer.
Sostituire il codice XAML generato automaticamente in Window1.xaml con il codice XAML seguente.
<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>Nell'editor del codice aprire Window1.xaml.cs.
Sostituire il codice in Window1.xaml.cs con il codice seguente.
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 Classusing 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); } } }
Vedere anche
Attività
Esempio di hosting di controlli Windows Form in Windows Presentation Foundation
Concetti
Procedura dettagliata: hosting di controlli di Windows Presentation Foundation in Windows Form
Controlli Windows Form e controlli WPF equivalenti