How to add windows forms control in WPF

How to add windows forms control in WPF

I will describe how we can use windows forms controls in WPF application.
I’ll assume I have a WPF Application and I want to use DateTimePicker Control, how I can do that in WPF Application?

1- Create WPF Application.
2- Add Stackpanel control.
3- in Solution Explorer Add Reference for:

  • System.Windows.Forms
  • WindowsFormsIntegration
using WindowsControls = System.Windows.Forms;
using WindowsIntegration = System.Windows.Forms.Integration;

The below method will Add DateTimePacker Control in WPF:

private void AddDateTimePacker(int x)
{
  for (int i = 0; i < x; i++)
  {
  WindowsIntegration.WindowsFormsHost host = new WindowsIntegration.WindowsFormsHost();
  WindowsControls.DateTimePicker dateTime = new WindowsControls.DateTimePicker();

  dateTime.Name = "DateTimePicker" + i;
  dateTime.Left = 0;
  dateTime.Top = 0;
  dateTime.Text = string.Empty;

  //Add control to stack panel
  host.Child = dateTime;
  stackPanel1.Children.Add(host);
  }
}

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *