Visual Studio: How to write Editor Extensions with WPF Visual Studio: How to write Editor Extensions with WPF wpf wpf

Visual Studio: How to write Editor Extensions with WPF


This is explained in detail here: WPF in Visual Studio 2010 – Part 4 : Direct Hosting of WPF content

So, if you use the standard Extensibility / Custom Editor sample that comes with the Visual Studio SDK, what you can do to test it is this:

1) Modify the provided EditorFactory.cs file like this:

        // Create the Document (editor)        //EditorPane NewEditor = new EditorPane(editorPackage); // comment this line        WpfEditorPane NewEditor = new WpfEditorPane(); // add this line

2) create for example a WpfEditorPane.cs file like this:

[ComVisible(true)]public class WpfEditorPane : WindowPane, IVsPersistDocData{    private TextBox _text;    public WpfEditorPane()        : base(null)    {        _text = new TextBox(); // Note this is the standard WPF thingy, not the Winforms one        _text.Text = "hello world";        Content = _text; // use any FrameworkElement-based class here.    }    #region IVsPersistDocData Members    // NOTE: these need to be implemented properly! following is just a sample    public int Close()    {        return VSConstants.S_OK;    }    public int GetGuidEditorType(out Guid pClassID)    {        pClassID = Guid.Empty;        return VSConstants.S_OK;    }    public int IsDocDataDirty(out int pfDirty)    {        pfDirty = 0;        return VSConstants.S_OK;    }    public int IsDocDataReloadable(out int pfReloadable)    {        pfReloadable = 0;        return VSConstants.S_OK;    }    public int LoadDocData(string pszMkDocument)    {        return VSConstants.S_OK;    }    public int OnRegisterDocData(uint docCookie, IVsHierarchy pHierNew, uint itemidNew)    {        return VSConstants.S_OK;    }    public int ReloadDocData(uint grfFlags)    {        return VSConstants.S_OK;    }    public int RenameDocData(uint grfAttribs, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)    {        return VSConstants.S_OK;    }    public int SaveDocData(VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew, out int pfSaveCanceled)    {        pbstrMkDocumentNew = null;        pfSaveCanceled = 0;        return VSConstants.S_OK;    }    public int SetUntitledDocPath(string pszDocDataPath)    {        return VSConstants.S_OK;    }    #endregion}

Of course, you will have to implement all the editor logic (add interfaces, etc.) to mimic what's done in the Winforms sample, as what I provide here is really the minimal stuff for pure demonstration purposes.

NOTE: this whole "Content" thing only works starting with Visual Studio 2010 (so you need to make sure your project references Visual Studio 2010 assemblies, which should be the case if you start a project from scratch using Visual Studio 2010). Hosting WPF editors within Visual Studio 2008 is possible using System.Windows.Forms.Integration.ElementHost.


I'm not sure if the above is possible but a quick search did turn up this: http://karlshifflett.wordpress.com/2010/03/21/visual-studio-2010-xaml-editor-intellisense-presenter-extension/

and this:

http://blogs.msdn.com/b/visualstudio/archive/2009/12/09/building-and-publishing-an-extension-for-visual-studio-2010.aspx

Failing that, could you not host a WPF control inside the winforms control using ElementHost? I know its a lame workaround but might allow you to develop in your favourite toolkit while you find a more permanent solution.

Best regards,


There is a sample VS Extension Package application source code on MSDN: Designer View Over XML Editor.

Description from site:
This Sample demonstrates how to create an extension with a WPF-based Visual Designer for editing XML files with a specific schema (XSD) in coordination with the Visual Studio XML Editor.

Admittedly solution is dedicated for VS2010 Extension Package, however it can be simply converted and attuned to VS2012 format - Target platform should be change to .NET 4.5, thereafter some references to VS2012 assemblies (v.11.0) should be added to build and run startup project:

Microsoft.VisualStudio.Shell.11.0.dll
Microsoft.VisualStudio.Shell.Immutable.11.0.dll
Microsoft.VisualStudio.Shell.Interop.11.0

In case of problems, please visit Q & A section of the site.