Export (0) Print
Expand All
4 out of 11 rated this helpful - Rate this topic

Walkthrough: Hosting a Windows Forms Control in WPF

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 code.

For a complete code listing of the tasks shown in this walkthrough, see Hosting a Windows Forms Control in WPF 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 HostingWfInWpf.

  2. Add references to the following assemblies.

    • WindowsFormsIntegration

    • System.Windows.Forms

  3. Open MainWindow.xaml in the WPF Designer.

  4. Name the Grid element grid1.

    <Grid Name="grid1">
    
    </Grid>
    
  5. In Design view or XAML view, select the Window element.

  6. In the Properties window, click the Events tab.

  7. Double-click the Loaded event.

  8. Insert the following code to handle the Loaded event.

    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);
    }
    
  9. At the top of the file, add the following Imports or using statement.

    using System.Windows.Forms;
    
  10. Press F5 to build and run the application.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.