逐步解說:使用 XAML 在 Windows Presentation Foundation 中裝載 Windows Form 控制項
更新:2007 年 11 月
WPF 提供許多具有豐富功能集的控制項。但是,您有時候可能會想要在 WPF 頁面上使用 Windows Form 控制項。例如,您可能對現有的 Windows Form 控制項做了大筆投資,或擁有具備獨一無二功能的 Windows Form 控制項。
本逐步解說顯示如何使用 XAML 在 WPF 頁面上裝載 (Host) Windows Form System.Windows.Forms.MaskedTextBox 控制項。
如需本逐步說明中所說明之工作的完整程式碼清單,請參閱使用 XAML 在 Windows Presentation Foundation 中裝載 Windows Form 控制項範例。
注意:根據您目前使用的設定或版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中描述的不同。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定。
必要條件
您需要下列元件才能完成此逐步解說:
- Visual Studio 2008
裝載 Windows Form 控制項
裝載 MaskedTextBox 控制項
建立名為 HostingWfInWpf 的 WPF 應用程式專案。
在 [方案總管中],加入名為 WindowsFormsIntegration.dll 之 WindowsFormsIntegration 組件的參考。
在 [方案總管] 中,加入名為 System.Windows.Forms.dll 之 Windows Form 組件的參考。
在 WPF 設計工具中開啟 Window1.xaml。
將 Window1.xaml 中的程式碼取代成下列範例中的程式碼。
wf 命名空間對應會建立包含這個 Windows Form 控制項的 DLL 的參考。MaskedTextBox 控制項會建立為 WindowsFormsHost 控制項的子系。
<Window x:Class="Window1" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Title="HostingWfInWpf" > <Grid> <WindowsFormsHost> <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/> </WindowsFormsHost> </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" xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Title="HostingWfInWpf" > <Grid> <WindowsFormsHost> <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/> </WindowsFormsHost> </Grid> </Window>
請參閱
工作
逐步解說:在 Windows Presentation Foundation 中裝載 Windows Form 控制項
使用 XAML 在 Windows Presentation Foundation 中裝載 Windows Form 控制項範例
概念
逐步解說:在 Windows Presentation Foundation 中裝載 Windows Form 複合控制項
逐步解說:在 Windows Form 中裝載 Windows Presentation Foundation 控制項