チュートリアル: WPF での Windows フォーム コントロールのホスト
更新 : 2010 年 8 月
WPF には、さまざまな機能を持つコントロールが多数あります。 しかし、独自の WPF ページの Windows Forms コントロールを使用する方が望ましい場合があります。 たとえば、既存の Windows Forms コントロールに多くの手間と時間がかかっている場合や、固有の機能を備えた Windows Forms コントロールがある場合などです。
このチュートリアルでは、コードを使用して Windows Formsの System.Windows.Forms.MaskedTextBox コントロールを WPF ページでホストする方法を示します。
このチュートリアルで示すタスクの完全なコード一覧については、WPF での Windows フォーム コントロールのホストのサンプルを参照してください。
必須コンポーネント
このチュートリアルを実行するには、次のコンポーネントが必要です。
- Visual Studio 2010
Windows Forms Control のホスト
MaskedTextBox コントロールをホストするには
HostingWfInWpf という名前の WPF アプリケーション プロジェクトを作成します。
次のアセンブリへの参照を追加します。
WindowsFormsIntegration
System.Windows.Forms
WPF デザイナーで MainWindow.xaml を開きます。
Grid 要素に grid1 という名前を付けます。
<Grid Name="grid1"> </Grid>
デザイン ビューまたは XAML ビューで、Window 要素を選択します。
プロパティ ウィンドウの [イベント] タブをクリックします。
Loaded イベントをダブルクリックします。
Loaded イベントを処理するには、次のコードを挿入します。
Private Sub Window_Loaded(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
private void Window_Loaded(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); }
ファイルの冒頭に、次の Imports ステートメントまたは using ステートメントを追加します。
Imports System.Windows.Forms
using System.Windows.Forms;
F5 キーを押してアプリケーションをビルドし、実行します。
参照
処理手順
チュートリアル: WPF での、XAML を使用した Windows フォーム コントロールのホスト
参照
概念
チュートリアル: WPF での Windows フォーム複合コントロールのホスト
チュートリアル: Windows フォームでの WPF 複合コントロールのホスト
Windows フォーム コントロールおよび同等の WPF コントロール
その他の技術情報
WPF での Windows フォーム コントロールのホストのサンプル
履歴の変更
日付 |
履歴 |
理由 |
---|---|---|
2010 年 8 月 |
Visual Studio 2010 用に更新。 |
カスタマー フィードバック |