チュートリアル : WindowsFormsHost 要素を使用したプロパティの割り当て

更新 : 2007 年 11 月

このチュートリアルでは、PropertyMap プロパティを使用して、ホストされている Windows フォーム コントロールの対応するプロパティに WPF プロパティを割り当てる方法を示します。

このチュートリアルでは、以下のタスクを行います。

  • プロジェクトの作成。

  • アプリケーション レイアウトの定義。

  • 新しいプロパティ マッピングの定義。

  • 既定のプロパティ マッピングの削除。

  • 既定のプロパティ割り当ての置き換え。

  • 既定のプロパティ マッピングの拡張。

このチュートリアルで示すタスクの完全なコード一覧については、「WindowsFormsHost 要素を使用したプロパティの割り当てのサンプル」を参照してください。

終了すると、ホストされている Windows フォーム コントロールの対応するプロパティに WPF プロパティを割り当てることができるようになります。

メモ   使用している設定またはエディションによっては、ヘルプの記載と異なるダイアログ ボックスやメニュー コマンドが表示される場合があります。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。

前提条件

このチュートリアルを完了するには、次のコンポーネントが必要です。

  • Visual Studio 2008.

プロジェクトの作成

プロジェクトを作成し、設定するには

  1. PropertyMappingWithWfh という名前の WPF アプリケーション プロジェクトを作成します。

  2. ソリューション エクスプローラで、WindowsFormsIntegration.dll という名前の WindowsFormsIntegration アセンブリへの参照を追加します。

  3. ソリューション エクスプローラで、System.Drawing および System.Windows.Forms アセンブリへの参照を追加します。

アプリケーション レイアウトの定義

WPF ベースのアプリケーションは、WindowsFormsHost 要素を使用して Windows フォーム コントロールをホストします。

アプリケーション レイアウトを定義するには

  1. WPF デザイナで Window1.xaml を開きます。

  2. 既存のコードを次のコードに置き換えます。

    <Window x:Class="Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="PropertyMappingWithWfh" Height="300" Width="300"
        Loaded="WindowLoaded">
      <DockPanel Name="panel1" LastChildFill="True">
        <WindowsFormsHost Name="wfHost" DockPanel.Dock="Left" SizeChanged="Window1_SizeChanged" FontSize="20" />
      </DockPanel>
    </Window>
    
    <Window x:Class="PropertyMappingWithWfh.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="PropertyMappingWithWfh" Height="300" Width="300"
        Loaded="WindowLoaded">
      <DockPanel Name="panel1" LastChildFill="True">
        <WindowsFormsHost Name="wfHost" DockPanel.Dock="Left" SizeChanged="Window1_SizeChanged" FontSize="20" />
      </DockPanel>
    </Window>
    
  3. コード エディタで Window1.xaml.cs を開きます。

  4. ファイルの先頭に次の名前空間をインポートします。

    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Windows.Forms
    Imports System.Windows.Forms.Integration
    
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    using System.Windows.Forms.Integration;
    

新しいプロパティ割り当ての定義

WindowsFormsHost 要素には、既定のプロパティ割り当てがいくつか用意されています。新しいプロパティ割り当てを追加するには、WindowsFormsHost 要素の PropertyMapAdd メソッドを呼び出します。

新しいプロパティ割り当てを定義するには

  • Window1 クラスの定義に次のコードをコピーします。

    ' The AddClipMapping method adds a custom mapping 
    ' for the Clip property.
    Private Sub AddClipMapping()
    
        wfHost.PropertyMap.Add( _
            "Clip", _
            New PropertyTranslator(AddressOf OnClipChange))
    
    End Sub
    
    ' The OnClipChange method assigns an elliptical clipping 
    ' region to the hosted control's Region property.
    Private Sub OnClipChange( _
    ByVal h As Object, _
    ByVal propertyName As String, _
    ByVal value As Object)
    
        Dim host As WindowsFormsHost = h
    
        Dim cb As System.Windows.Forms.CheckBox = host.Child
    
        If cb IsNot Nothing Then
            cb.Region = Me.CreateClipRegion()
        End If
    
    End Sub
    
    ' The Window1_SizeChanged method handles the window's 
    ' SizeChanged event. It calls the OnClipChange method explicitly 
    ' to assign a new clipping region to the hosted control.
    Private Sub Window1_SizeChanged( _
    ByVal sender As Object, _
    ByVal e As SizeChangedEventArgs)
    
        Me.OnClipChange(wfHost, "Clip", Nothing)
    
    End Sub
    
    ' The CreateClipRegion method creates a Region from an
    ' elliptical GraphicsPath.
    Private Function CreateClipRegion() As [Region] 
        Dim path As New GraphicsPath()
    
        path.StartFigure()
    
        path.AddEllipse(New System.Drawing.Rectangle( _
            0, _
            0, _
            wfHost.ActualWidth, _
            wfHost.ActualHeight))
    
        path.CloseFigure()
    
        Return New [Region](path)
    
    End Function
    
    // The AddClipMapping method adds a custom 
    // mapping for the Clip property.
    private void AddClipMapping()
    {
        wfHost.PropertyMap.Add(
            "Clip",
            new PropertyTranslator(OnClipChange));
    }
    
    // The OnClipChange method assigns an elliptical clipping 
    // region to the hosted control's Region property.
    private void OnClipChange(object h, String propertyName, object value)
    {
        WindowsFormsHost host = h as WindowsFormsHost;
        System.Windows.Forms.CheckBox cb = host.Child as System.Windows.Forms.CheckBox;
    
        if (cb != null)
        {
            cb.Region = this.CreateClipRegion();
        }
    }
    
    // The Window1_SizeChanged method handles the window's 
    // SizeChanged event. It calls the OnClipChange method explicitly 
    // to assign a new clipping region to the hosted control.
    private void Window1_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        this.OnClipChange(wfHost, "Clip", null);
    }
    
    // The CreateClipRegion method creates a Region from an
    // elliptical GraphicsPath.
    private Region CreateClipRegion()
    {   
        GraphicsPath path = new GraphicsPath();
    
        path.StartFigure(); 
    
        path.AddEllipse(new System.Drawing.Rectangle( 
            0, 
            0, 
            (int)wfHost.ActualWidth, 
            (int)wfHost.ActualHeight ) );
    
        path.CloseFigure(); 
    
        return( new Region(path) );
    }
    

    AddClipMapping メソッドは、Clip プロパティの新しいマッピングを追加します。

    OnClipChange メソッドは、Clip プロパティを Windows フォームの Region プロパティに変換します。

    Window1_SizeChanged メソッドは、ウィンドウの SizeChanged イベントを処理し、アプリケーション ウィンドウに合わせてクリッピング領域のサイズを設定します。

既定のプロパティ マッピングの削除

既定のプロパティ割り当てを削除するには、WindowsFormsHost 要素の PropertyMapRemove メソッドを呼び出します。

既定のプロパティ マッピングを削除するには

  • Window1 クラスの定義に次のコードをコピーします。

    ' The RemoveCursorMapping method deletes the default
    ' mapping for the Cursor property.
    Private Sub RemoveCursorMapping()
        wfHost.PropertyMap.Remove("Cursor")
    End Sub
    
    // The RemoveCursorMapping method deletes the default
    // mapping for the Cursor property.
    private void RemoveCursorMapping()
    {
        wfHost.PropertyMap.Remove("Cursor");
    }
    

    RemoveCursorMapping メソッドは、Cursor プロパティの既定のマッピングを削除します。

既定のプロパティ割り当ての置き換え

既定のプロパティ割り当てを置き換えるには、既定の割り当てを削除して、WindowsFormsHost 要素の PropertyMapAdd メソッドを呼び出します。

既定のプロパティ割り当てを置き換えるには

  • Window1 クラスの定義に次のコードをコピーします。

    ' The ReplaceFlowDirectionMapping method replaces the
    ' default mapping for the FlowDirection property.
    Private Sub ReplaceFlowDirectionMapping()
    
        wfHost.PropertyMap.Remove("FlowDirection")
    
        wfHost.PropertyMap.Add( _
            "FlowDirection", _
            New PropertyTranslator(AddressOf OnFlowDirectionChange))
    End Sub
    
    
    ' The OnFlowDirectionChange method translates a 
    ' Windows Presentation Foundation FlowDirection value 
    ' to a Windows Forms RightToLeft value and assigns
    ' the result to the hosted control's RightToLeft property.
    Private Sub OnFlowDirectionChange( _
    ByVal h As Object, _
    ByVal propertyName As String, _
    ByVal value As Object)
    
        Dim host As WindowsFormsHost = h
    
        Dim fd As System.Windows.FlowDirection = _
            CType(value, System.Windows.FlowDirection)
    
        Dim cb As System.Windows.Forms.CheckBox = host.Child
    
        cb.RightToLeft = IIf(fd = System.Windows.FlowDirection.RightToLeft, _
            RightToLeft.Yes, _
            RightToLeft.No)
    
    End Sub
    
    
    ' The cb_CheckedChanged method handles the hosted control's
    ' CheckedChanged event. If the Checked property is true,
    ' the flow direction is set to RightToLeft, otherwise it is
    ' set to LeftToRight.
    Private Sub cb_CheckedChanged( _
    ByVal sender As Object, _
    ByVal e As EventArgs)
    
        Dim cb As System.Windows.Forms.CheckBox = sender
    
        wfHost.FlowDirection = IIf(cb.CheckState = CheckState.Checked, _
        System.Windows.FlowDirection.RightToLeft, _
        System.Windows.FlowDirection.LeftToRight)
    
    End Sub
    
    // The ReplaceFlowDirectionMapping method replaces the  
    // default mapping for the FlowDirection property.
    private void ReplaceFlowDirectionMapping()
    {
        wfHost.PropertyMap.Remove("FlowDirection");
    
        wfHost.PropertyMap.Add(
            "FlowDirection",
            new PropertyTranslator(OnFlowDirectionChange));
    }
    
    // The OnFlowDirectionChange method translates a 
    // Windows Presentation Foundation FlowDirection value 
    // to a Windows Forms RightToLeft value and assigns
    // the result to the hosted control's RightToLeft property.
    private void OnFlowDirectionChange(object h, String propertyName, object value)
    {
        WindowsFormsHost host = h as WindowsFormsHost;
        System.Windows.FlowDirection fd = (System.Windows.FlowDirection)value;
        System.Windows.Forms.CheckBox cb = host.Child as System.Windows.Forms.CheckBox;
    
        cb.RightToLeft = (fd == System.Windows.FlowDirection.RightToLeft ) ? 
            RightToLeft.Yes : RightToLeft.No;
    }
    
    // The cb_CheckedChanged method handles the hosted control's
    // CheckedChanged event. If the Checked property is true,
    // the flow direction is set to RightToLeft, otherwise it is
    // set to LeftToRight.
    private void cb_CheckedChanged(object sender, EventArgs e)
    {
        System.Windows.Forms.CheckBox cb = sender as System.Windows.Forms.CheckBox;
    
        wfHost.FlowDirection = ( cb.CheckState == CheckState.Checked ) ? 
                System.Windows.FlowDirection.RightToLeft : 
                System.Windows.FlowDirection.LeftToRight;
    }
    

    ReplaceFlowDirectionMapping メソッドは、FlowDirection プロパティの既定の割り当てを置き換えます。

    OnFlowDirectionChange メソッドは、FlowDirection プロパティを Windows フォームの RightToLeft プロパティに変換します。

    cb_CheckedChanged メソッドは、CheckBox コントロールの CheckedChanged イベントを処理します。また、CheckState プロパティの値に基づいて FlowDirection プロパティを割り当てます。

既定のプロパティ マッピングの拡張

既定のプロパティ マッピングを使用したり、それをユーザー独自のマッピングで拡張することもできます。

既定のプロパティ マッピングを拡張するには

  • Window1 クラスの定義に次のコードをコピーします。

    ' The ExtendBackgroundMapping method adds a property
    ' translator if a mapping already exists.
    Private Sub ExtendBackgroundMapping() 
        If wfHost.PropertyMap("Background") IsNot Nothing Then
    
            wfHost.PropertyMap("Background") = PropertyTranslator.Combine( _
            wfHost.PropertyMap("Background"), _
            PropertyTranslator.CreateDelegate( _
                GetType(PropertyTranslator), _
                Me, _
                "OnBackgroundChange"))
        End If
    
    End Sub
    
    
    ' The OnBackgroundChange method assigns a specific image 
    ' to the hosted control's BackgroundImage property.
    Private Sub OnBackgroundChange(ByVal h As Object, ByVal propertyName As String, ByVal value As Object) 
        Dim host As WindowsFormsHost = h 
        Dim cb As System.Windows.Forms.CheckBox = host.Child 
        Dim b As ImageBrush = value 
    
        If Not (b Is Nothing) Then
            cb.BackgroundImage = New System.Drawing.Bitmap("C:\WINDOWS\Santa Fe Stucco.bmp")
        End If
    
    End Sub
    
    // The ExtendBackgroundMapping method adds a property
    // translator if a mapping already exists.
    private void ExtendBackgroundMapping()
    {
        if (wfHost.PropertyMap["Background"] != null)
        {
            wfHost.PropertyMap["Background"] += new PropertyTranslator(OnBackgroundChange);
        }
    }
    
    // The OnBackgroundChange method assigns a specific image 
    // to the hosted control's BackgroundImage property.
    private void OnBackgroundChange(object h, String propertyName, object value)
    {
        WindowsFormsHost host = h as WindowsFormsHost;
        System.Windows.Forms.CheckBox cb = host.Child as System.Windows.Forms.CheckBox;
        ImageBrush b = value as ImageBrush;
    
        if (b != null)
        {
            cb.BackgroundImage = new System.Drawing.Bitmap(@"C:\WINDOWS\Santa Fe Stucco.bmp");
        }
    }
    

    ExtendBackgroundMapping メソッドは、カスタム プロパティ トランスレータを既存の Background プロパティ マッピングに追加します。

    OnBackgroundChange メソッドは、特定のイメージをホストされているコントロールの BackgroundImage プロパティに割り当てます。OnBackgroundChange メソッドは、既定のプロパティ マッピングの適用後に呼び出されます。

プロパティ マッピングの初期化

プロパティの割り当てを設定するには、Loaded イベント ハンドラで前述のメソッドを呼び出します。

プロパティ マッピングを初期化するには

  1. Window1 クラスの定義に次のコードをコピーします。

    ' The WindowLoaded method handles the Loaded event.
    ' It enables Windows Forms visual styles, creates 
    ' a Windows Forms checkbox control, and assigns the
    ' control as the child of the WindowsFormsHost element. 
    ' This method also modifies property mappings on the 
    ' WindowsFormsHost element.
    Private Sub WindowLoaded( _
    ByVal sender As Object, _
    ByVal e As RoutedEventArgs)
    
        System.Windows.Forms.Application.EnableVisualStyles()
    
        ' Create a Windows Forms checkbox control and assign 
        ' it as the WindowsFormsHost element's child.
        Dim cb As New System.Windows.Forms.CheckBox()
        cb.Text = "Windows Forms checkbox"
        cb.Dock = DockStyle.Fill
        cb.TextAlign = ContentAlignment.MiddleCenter
        AddHandler cb.CheckedChanged, AddressOf cb_CheckedChanged
        wfHost.Child = cb
    
        ' Replace the default mapping for the FlowDirection property.
        Me.ReplaceFlowDirectionMapping()
    
        ' Remove the mapping for the Cursor property.
        Me.RemoveCursorMapping()
    
        ' Add the mapping for the Clip property.
        Me.AddClipMapping()
    
        ' Add another mapping for the Background property.
        Me.ExtendBackgroundMapping()
    
        ' Cause the OnFlowDirectionChange delegate to be called.
        wfHost.FlowDirection = System.Windows.FlowDirection.LeftToRight
    
        ' Cause the OnClipChange delegate to be called.
        wfHost.Clip = New RectangleGeometry()
    
        ' Cause the OnBackgroundChange delegate to be called.
        wfHost.Background = New ImageBrush()
    
    End Sub
    
    // The WindowLoaded method handles the Loaded event.
    // It enables Windows Forms visual styles, creates 
    // a Windows Forms checkbox control, and assigns the
    // control as the child of the WindowsFormsHost element. 
    // This method also modifies property mappings on the 
    // WindowsFormsHost element.
    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.Application.EnableVisualStyles();
    
        // Create a Windows Forms checkbox control and assign 
        // it as the WindowsFormsHost element's child.
        System.Windows.Forms.CheckBox cb = new System.Windows.Forms.CheckBox();
        cb.Text = "Windows Forms checkbox";
        cb.Dock = DockStyle.Fill;
        cb.TextAlign = ContentAlignment.MiddleCenter;
        cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
        wfHost.Child = cb;
    
        // Replace the default mapping for the FlowDirection property.
        this.ReplaceFlowDirectionMapping();
    
        // Remove the mapping for the Cursor property.
        this.RemoveCursorMapping();
    
        // Add the mapping for the Clip property.
        this.AddClipMapping();
    
        // Add another mapping for the Background property.
        this.ExtendBackgroundMapping();
    
        // Cause the OnFlowDirectionChange delegate to be called.
        wfHost.FlowDirection = System.Windows.FlowDirection.LeftToRight;
    
        // Cause the OnClipChange delegate to be called.
        wfHost.Clip = new RectangleGeometry();
    
        // Cause the OnBackgroundChange delegate to be called.
        wfHost.Background = new ImageBrush();
    }
    

    WindowLoaded メソッドは Loaded イベントを処理し、次の初期化を実行します。

    • Windows フォームCheckBox コントロールを作成します。

    • このチュートリアルで定義したメソッドを呼び出して、プロパティ マッピングを設定します。

    • マップされたプロパティに初期値を割り当てます。

  2. F5 キーを押してアプリケーションをビルドし、実行します。チェック ボックスをオンにして、FlowDirection 割り当ての結果を確認します。チェック ボックスをオンにすると、レイアウトの左右の向きが逆になります。

参照

処理手順

チュートリアル : Windows Presentation Foundation での Windows フォーム コントロールのホスト

概念

Windows フォームと WPF プロパティの割り当て

参照

WindowsFormsHost.PropertyMap

ElementHost.PropertyMap

WindowsFormsHost

その他の技術情報

WPF デザイナ

移行と相互運用性

移行と相互運用性のサンプル