using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace ClarionAddins.WpfTest
{
public partial class WpfTestForm : Form
{
public WpfTestForm ()
{
InitializeComponent();
AddWpfControl();
}
private void AddWpfControl()
{
// You need a full AssemblyQualifiedName for the elementHost class
string elementHostQualifiedName = "System.Windows.Forms.Integration.ElementHost, WindowsFormsIntegration, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
// Using this it is possible to load the ElementHost class at runtime and create an instance we can use
Type elemHostType = Type.GetType(elementHostQualifiedName, true);
object ctrlHost = Activator.CreateInstance(elemHostType);
// Dock this ElementHost into our WinForms control container
MethodInfo info = elemHostType.GetMethod("Dock");
elemHostType.GetProperty("Dock").SetValue(ctrlHost, DockStyle.Fill, null);
// Finally, add the ElementHost to the WinForms list of controls
this.Controls.Add((Control)ctrlHost);
// Now, load the WPF UserControl to be displayed
string currentPath = Path.GetDirectoryName((new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath);
string userControlPath = Path.Combine(currentPath, "WpfControlLibrary.dll");
Assembly assembly = Assembly.LoadFrom(userControlPath);
Type userControlType = assembly.GetType("WpfControlLibrary.MyUserControl", true);
// Create an instance of the UserControl and call the InitializeComponent method
object userControl = Activator.CreateInstance(userControlType);
info = userControlType.GetMethod("InitializeComponent");
info.Invoke(userControl, null);
// Last of all, attach the UserControl to the ElementHost !
elemHostType.GetProperty("Child").SetValue(ctrlHost, userControl, null);
}
}
}