共用方式為


逐步解說:使用 XAML 在 WPF 中裝載 Windows Form 控制項

更新:2010 年 8 月

WPF 提供許多具有豐富功能集的控制項。 但是,您有時候可能會想要在 WPF 頁面上使用 Windows Forms 控制項。 例如,您可能對現有的 Windows Forms 控制項做了大筆投資,或擁有具備獨一無二功能的 Windows Forms 控制項。

本逐步解說顯示如何使用 XAML 在 WPF 頁面上裝載 (Host) Windows Form System.Windows.Forms.MaskedTextBox 控制項。

如需這個逐步解說中所說明之工作的完整程式碼清單,請參閱使用 XAML 在 WPF 中裝載 Windows Form 控制項範例 (英文)。

必要條件

您需要下列元件才能完成此逐步解說:

  • Visual Studio 2010。

裝載 Windows Form 控制項

若要裝載 MaskedTextBox 控制項

  1. 建立名稱為 HostingWfInWpfWithXaml 的 WPF 應用程式專案。

  2. 加入下列組件的參考。

    • WindowsFormsIntegration

    • System.Windows.Forms

  3. 在 WPF 設計工具中開啟 MainWindow.xaml。

  4. Window 項目中,加入下列命名空間對應。 wf 命名空間對應會建立組件的參考,這個組件包含 Windows Forms 控制項。

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    
  5. Grid 項目中,加入下列 XAML。

    MaskedTextBox 控制項會建立為 WindowsFormsHost 控制項的子系。

        <Grid>
    
            <WindowsFormsHost>
                <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
            </WindowsFormsHost>
    
        </Grid>
    
    
  6. 按 F5 建置並執行應用程式。

請參閱

工作

逐步解說:在 WPF 中裝載 Windows Form 控制項

參考

ElementHost

WindowsFormsHost

概念

逐步解說:在 WPF 中裝載 Windows Form 複合控制項

逐步解說:在 Windows Form 中裝載 WPF 複合控制項

Windows Form 控制項和對等 WPF 控制項

其他資源

WPF 設計工具

使用 XAML 在 WPF 中裝載 Windows Form 控制項範例

變更記錄

日期

記錄

原因

2010 年 8 月

更新成 Visual Studio 2010 適用的內容。

客戶回函。