WPF to Windows Forms Interop is one of the development strategies that will take existing applications and bring them to the future. WPF currently provides the ability to fairly easily create new experiences for visualizing applications. Windows Forms has the tools, the frameworks, and years of development effort invested into its existence. If you have a legacy application in Windows Forms and wish to add new experiences to it or better ways to visualize information; then Interop is the way to go.
Infragistics has released its first WPF controls and included in this suite of controls are new metaphors for visualizing information or for that matter creating navigation items. It makes perfect sense to include these controls inside of existing Windows Forms Applications.
To begin we'll focus on the first challenge in integration, which is to get the WPF control (in this case the XamDataPresenter) into the Windows Forms Application. First you need to add references to the Microsoft WPF assemblies that handle WPF, the assemblies are as follows:
After adding references to the Microsoft assemblies, next add references to the Infragistics assemblies which include:
Next, you'll need to add code to host the WPF control inside the Windows Forms application. In the case of this sample, I'll perform the core logic inside of the Form_Load event. The main piece of code revolves around the ElementHost object; which is a container that is used to hold an element [e.g. WPF Control].
For the purpose of this sample, I'll also create an instance of the XamDataPresenter and assign its DataSource property. Then, I'll need to associate the XamDataPresenter as a child of the ElementHost object. The code will look something like this:
…
using System.Windows.Forms.Integration;
namespace WindowsForms_WPF_InteropDemo
{
public partial class Main: Form { private ElementHost XamHost; private Infragistics.Windows.DataPresenter.XamDataPresenter XamDP; … private void Form1_Load(object sender, EventArgs e) { XamHost = new ElementHost(); XamHost.Dock = DockStyle.Fill; this.Controls.Add(XamHost); XamDP = new Infragistics.Windows.DataPresenter.XamDataPresenter(); XamDP.DataSource = this.vSalesPersonSalesByFiscalYearsBindingSource; XamHost.Child = XamDP; } …
public partial class Main: Form
private ElementHost XamHost; private Infragistics.Windows.DataPresenter.XamDataPresenter XamDP; … private void Form1_Load(object sender, EventArgs e) { XamHost = new ElementHost(); XamHost.Dock = DockStyle.Fill; this.Controls.Add(XamHost); XamDP = new Infragistics.Windows.DataPresenter.XamDataPresenter(); XamDP.DataSource = this.vSalesPersonSalesByFiscalYearsBindingSource; XamHost.Child = XamDP; }
private ElementHost XamHost;
private Infragistics.Windows.DataPresenter.XamDataPresenter XamDP;
private void Form1_Load(object sender, EventArgs e)
XamHost = new ElementHost(); XamHost.Dock = DockStyle.Fill; this.Controls.Add(XamHost); XamDP = new Infragistics.Windows.DataPresenter.XamDataPresenter(); XamDP.DataSource = this.vSalesPersonSalesByFiscalYearsBindingSource; XamHost.Child = XamDP;
XamHost = new ElementHost();
XamHost.Dock = DockStyle.Fill;
this.Controls.Add(XamHost);
XamDP = new Infragistics.Windows.DataPresenter.XamDataPresenter();
XamDP.DataSource = this.vSalesPersonSalesByFiscalYearsBindingSource;
XamHost.Child = XamDP;
}
This implementation is fairly easy to achieve, gives us access to the full XamDataPresenter object model and it was only a few lines of code. This will enable developers to fully target the control inside of Visual Studio.