Thursday, July 23, 2015

Host Winform control in WPF

How to host Winform control in wpf?
To host the MaskedTextBox control
  1. Create a WPF Application project named HostingWfInWpf.
  2. Add references to the following assemblies.
WindowsFormsIntegration
System.Windows.Forms
  1. Open MainWindow.xaml in the WPF Designer.
  2. Name the Grid element grid1.
<Grid Name="grid1"/>
  1. In Design view or XAML view, select the Window element.
  2. In the Properties window, click the Events tab.
  3. Double-click the Loaded event.
  4. 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);
}
At the top of the file, add the following Imports or using statement.

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

No comments:

Post a Comment