Export (0) Print
Expand All

Walkthrough: Hosting a Windows Forms Control in WPF by Using XAML

WPF provides many controls with a rich feature set. However, you may sometimes want to use Windows Forms controls on your WPF pages. For example, you may have a substantial investment in existing Windows Forms controls, or you may have a Windows Forms control that provides unique functionality.

This walkthrough shows you how to host a Windows Forms System.Windows.Forms.MaskedTextBox control on a WPF page by using XAML.

For a complete code listing of the tasks shown in this walkthrough, see Hosting a Windows Forms Control in WPF by Using XAML Sample.

You need the following components to complete this walkthrough:

  • Visual Studio 2010.

To host the MaskedTextBox control

  1. Create a WPF Application project named HostingWfInWpfWithXaml.

  2. Add references to the following assemblies.

    • WindowsFormsIntegration

    • System.Windows.Forms

  3. Open MainWindow.xaml in the WPF Designer.

  4. In the Window element, add the following namespace mapping. The wf namespace mapping establishes a reference to the assembly that contains the Windows Forms control.

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    
  5. In the Grid element add the following XAML.

    The MaskedTextBox control is created as a child of the WindowsFormsHost control.

    
    	<Grid>
    
    		<WindowsFormsHost>
    			<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
    		</WindowsFormsHost>
    
    	</Grid>
    
    
    
    
  6. Press F5 to build and run the application.

Show:
© 2014 Microsoft