Home > Extensibility, Visual Studio > Create a WPF based ToolWindow in your Add-In

Create a WPF based ToolWindow in your Add-In

One of the big changes in Visual Studio 2010 is the introduction of the WPF based UI which means that (amongst other things) you can now host WPF windows directly within the IDE without having to wrap them inside WinForm controls.

Unfortunately you may notice that if you create a simple WPF UserControl and then try to create a ToolWindow from it using CreateToolWindow2, you’ll run into the following Managed Debugging Assistant:

Managed Debugging Assistant 'NonComVisibleBaseClass' has detected a problem in 'c:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe'.

Additional Information: A QueryInterface call was made requesting the default IDispatch interface of COM visible managed class 'WPFUserControlAddIn.WPFToolWindow'. 

However since this class does not have an explicit default interface and derives from non COM visible class 'System.Windows.Controls.UserControl', the QueryInterface call will fail. 

This is done to prevent the non COM visible base class from being constrained by the COM versioning rules.

If you follow through this error message it will lead you to the MSDN page for this error (here) and from there to the page that holds the solution.

The Solution

Add the following to your [yourfile].xaml.cs around your class declaration:

    using System.Runtime.InteropServices;
    using stdole;

    class [yourclass] : UserControl, IDispatch

Note! Don’t forget the addition of the IDispatch implementation (there’s nothing to implement, don’t worry). Without it you’ll get "InvalidCastException" from mscordb.lib and a null in your ref object (the last parameter to CreateToolWindow2.

Got a Sample For Us?

You bet – grab the sample solution. All you need to do is extract, add the .addin file to you AddIns directory (under Documents\Visual Studio 2010\Addins) and away you go.

Categories: Extensibility, Visual Studio Tags:
  1. 62316e
    January 18th, 2011 at 09:10 | #1

    Sample solution url is broken!

  2. R
    May 16th, 2012 at 07:44 | #2

    It worked, thanks a lot!

  3. Nazaf
    September 30th, 2012 at 15:11 | #3

    It works but when I dock the tool window. the title is empty unlike Windows Forms tool window. any ideas how to fix this ???

  4. October 21st, 2012 at 10:29 | #4

    I’m not sure, I haven’t used the dock – though none of this process should be required in the newest VS and it should “just work”…

  1. No trackbacks yet.