.NET Framework Class Library
WindowsFormsHost Class

An element that allows you to host a Windows Forms control on a WPF page.

Namespace:  System.Windows.Forms.Integration
Assembly:  WindowsFormsIntegration (in WindowsFormsIntegration.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax

Visual Basic (Declaration)
<ContentPropertyAttribute("Child")> _
Public Class WindowsFormsHost _
    Inherits HwndHost _
    Implements IKeyboardInputSink
Visual Basic (Usage)
Dim instance As WindowsFormsHost
C#
[ContentPropertyAttribute("Child")]
public class WindowsFormsHost : HwndHost, 
    IKeyboardInputSink
Visual C++
[ContentPropertyAttribute(L"Child")]
public ref class WindowsFormsHost : public HwndHost, 
    IKeyboardInputSink
JScript
public class WindowsFormsHost extends HwndHost implements IKeyboardInputSink
XAML Object Element Usage
<WindowsFormsHost.../>
Remarks

Use the WindowsFormsHost element to place a Windows Forms control within your WPF element or page.

To host a WPF element in a Windows Forms control or form, use the ElementHost control.

NoteNote:

WindowsFormsIntegration.dll is installed with the Windows Presentation Foundation (WPF) assemblies. The default location for the assembly is %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.

To host a Windows Forms control in a WPF element, you must assign the Windows Forms control to the Child property.

Use the PropertyMap property to assign custom mappings between a WindowsFormsHost element and its hosted Windows Forms control. For more information, see Windows Forms and WPF Property Mapping.

Examples

The following code example demonstrates how to use a WindowsFormsHost element to host a System.Windows.Forms..::.MaskedTextBox control. For more information, see Walkthrough: Hosting a Windows Forms Control in Windows Presentation Foundation by Using XAML.

XAML
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
    Title="HostingWfInWpf"
    >

  <Grid>

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

  </Grid>


</Window>
XAML
<Window x:Class="HostingWfInWpf.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
    Title="HostingWfInWpf"
    >

    <Grid>

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

    </Grid>

    
</Window>
Inheritance Hierarchy

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Interop..::.HwndHost
              System.Windows.Forms.Integration..::.WindowsFormsHost
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Tags :


Community Content

zainnab
Hosting a Windows Form Control in WPF

This example uses a MonthCalendar Control to show interop between WPF and Windows Form Control.

Interface Code:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration">
<Grid>
<wfi:WindowsFormsHost Margin="15,15,12.5,97.5">
            <wf:MonthCalendar Name="myMontCalendar" ShowWeekNumbers="True"  DateChanged="MonthCalendar_DateChanged"/>
        </wfi:WindowsFormsHost>
<TextBox Height="23" Margin="88.75,0,68.75,12.5" Name="TextBox1" VerticalAlignment="Bottom" />
</Grid>
</Window>

Code for the MonthCalendar_DateChanged Event:

 Private Sub MonthCalendar_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs)
TextBox1.Text = sender.SelectionStart.ToShortDateString()
End Sub
Tags :

Page view tracker