Hosting a WPF control inside a ToolWindow
If you have downloaded StickyNotes you already noticed that the main toolwindow is hosting a WPF control.
Let see the steps needed to host a WPF control in a VS Toolwindow:
- Add a reference to C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll
- Add a WPF user control
- Add a class that inherits from ToolWindowPane (our ToolWindow)
- Modify the ToolWindowPane class as follows:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using Microsoft.VisualStudio.Shell;
[Guid("2070F92C-0843-4ba2-A46E-D559B1F03B2F")]
public class MyToolwindow : ToolWindowPane
{
private ElementHost elementHost;
public MyToolwindow:
base(null)
{
this.Caption = "My Tool Window";
}
protected override void Initialize()
{
base.Initialize();
elementHost = new ElementHost();
elementHost.Child = new MyWPFControl();
}
override public IWin32Window Window
{
get
{
return (IWin32Window)elementHost;
}
}
}
That's all you need,
Pablo