Pablo Galiano : Hosting a WPF control inside a ToolWindow

Subscriptions

<September 2010>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

News

Subscribe to Pablo Galiano by Email

Post Categories

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:

  1. Add a reference to C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll
  2. Add a WPF user control
  3. Add a class that inherits from ToolWindowPane (our ToolWindow)
  4. 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

posted on Thursday, March 13, 2008 5:50 AM by pga

What do you think?

(required) 
required 
optional
required