Thursday, August 6, 2009

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);
}
}

1 comment:

  1. q. why you want to add .net control into wpf, alreay wpf has alot of best one, and thre's also 3rd party, use it man

    ReplyDelete