チュートリアル : Windows Presentation Foundation での Windows フォーム コントロールのホスト
更新 : 2007 年 11 月
WPF には、さまざまな機能を持つコントロールが多数あります。しかし、時に、独自の WPF ページの Windows フォーム コントロールを使用する方が望ましい場合があります。たとえば、既存の Windows フォーム コントロールに多くの手間と時間がかかっている場合や、固有の機能を備えた Windows フォーム コントロールがある場合などです。
このチュートリアルでは、コードを使用して Windows フォームの System.Windows.Forms.MaskedTextBox コントロールを WPF ページでホストする方法を示します。
このチュートリアルで示すタスクの完全なコード一覧については、「Windows Presentation Foundation での Windows フォーム コントロールのホストのサンプル」を参照してください。
メモ 使用している設定またはエディションによっては、ヘルプの記載と異なるダイアログ ボックスやメニュー コマンドが表示される場合があります。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。
前提条件
このチュートリアルを完了するには、次のコンポーネントが必要です。
- Visual Studio 2008.
Windows Forms Control のホスト
MaskedTextBox コントロールをホストするには
HostingWfInWpf という名前の WPF アプリケーション プロジェクトを作成します。
ソリューション エクスプローラで、WindowsFormsIntegration.dll という名前の WindowsFormsIntegration アセンブリへの参照を追加します。
ソリューション エクスプローラで、System.Windows.Forms.dll という名前の Windows フォーム アセンブリへの参照を追加します。
WPF デザイナで Window1.xaml を開きます。
Window1.xaml の自動的に生成された XAML を、次の XAML に置き換えます。
<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>
コード エディタで Window1.xaml.cs を開きます。
Window1.xaml.cs のコードを次のコードに置き換えます。
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 Class
using 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); } } }
参照
処理手順
チュートリアル : Windows Presentation Foundation での、XAML を使用した Windows フォーム コントロールのホスト
Windows Presentation Foundation での Windows フォーム コントロールのホストのサンプル
概念
チュートリアル : Windows Presentation Foundation での Windows フォーム複合コントロールのホスト
チュートリアル : Windows フォームでの Windows Presentation Foundation コントロールのホスト
Windows フォーム コントロールおよび同等の WPF コントロール