共用方式為


逐步解說:在 WPF 應用程式中裝載 Windows Form 控制項

本逐步解說示範如何使用 WPF Designer for Visual Studio,在 WPF 應用程式中裝載 Windows Form 協力廠商控制項。 如需 Windows Form 和 WPF 互通性的詳細資訊,請參閱移轉和互通性

這個逐步解說所說明的工作包括:

  • 建立專案。

  • 在 WPF 應用程式中,裝載 Windows Form 控制項。

注意事項注意事項

根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。 若要變更設定,請從 [工具] 功能表中選取 [匯入和匯出設定]。 如需詳細資訊,請參閱 使用設定

必要條件

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

  • Visual Studio 2010。

建立專案

第一步是建立主應用程式的專案。

若要建立專案

  1. 建立名為 "DemoApplication" 的 WPF 應用程式專案。 如需詳細資訊,請參閱 HOW TO:建立新的 WPF 應用程式專案

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

  2. 在 [方案總管] 中,加入名為 WindowsFormsIntegration.dll 的 WindowsFormsIntegration 組件之參考。

  3. 在 [方案總管] 中,加入名為 System.Windows.Forms.dll 之 Windows Form 組件的參考。

將 Windows Form 控制項裝載在 WPF 中

您要使用 WindowsFormsHost 項目,在 WPF 應用程式中裝載 Windows Form 控制項。

若要在 WPF 中裝載 Windows Form 控制項

  1. 在 MainWindow.xaml 的 [XAML] 檢視中,於開頭 Window 標記中插入下列命名空間對應,以匯入 Windows Form 命名空間。 如需詳細資訊,請參閱 HOW TO:將命名空間匯入 XAML

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    
  2. Grid 開頭標記之後,插入下列程式碼。

    <WindowsFormsHost>
        <wf:MaskedTextBox Mask="00/00/0000" />
    </WindowsFormsHost>
    

    代表已裝載之 Windows Form 控制項 (MaskedTextBox) 的區域隨即顯示在設計工具中。

後續步驟

您也可以將 WPF 控制項裝載在 Windows Form 中。 如需詳細資訊,請參閱使用 WPF 控制項

請參閱

工作

逐步解說:使用 WPF 設計工具裝載 Windows Form 使用者控制項

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

參考

WindowsFormsHost

ElementHost

其他資源

使用 WPF 設計工具中的控制項

移轉和互通性