locked
custom context menu in solution explorer RRS feed

  • Question

  • Hi,

    I need to accomplish a simple task (or so I would think) - I need to add a new right click option (context menu item) in Solution Explorer area in VS2010. and this item should only be enabled for .xaml files. in code behind of that click I need to access project context that this files belongs to and retrieve a list of all referenced assemblies and their respective file locations and contents of that .xaml file.

    I've gone through ton of tutorials, but most of them seem to be focusing on adding items to Tools menu and modifying Code Editor behaviour, but not a single coherent tutorial onadding conditional context item.

    I understand that accomplishing above would be a trivial task for someone experienced with VS SDK, I would greatly appreciate code examples.

    I prefer to do it with VS Addin as opposed to VS Package as I prefer to work with code more than xml.

    Thank you

    Thursday, August 25, 2011 5:32 PM

Answers

  • Using DTE and an AddIn vs VSCT and a package comes with tradeoffs, specifically perf is worse with DTE.  If that is fine in your situation then doing what you propose via DTE really isn't terribly different than what the samples you have read show.  The main differences would be

    1:  You need to fetch the proper context menu instead of MenuBar (which is the main menu) from the DTE.CommandBars collection.

    2:  As for dynamic visibility you need to implement IDteCommandTarget on your AddIn and handle the call back for the status of your menu, you likely would need to use IVsMonitorSelection or some DTE equivalent to fetch the active hierarchy and test if it is a XAML file.

    I don't have an example that does exactly what you want, but modifying an existing one to do the first part (place the command on the right menu) should be fairly simple, the second one is trickier but I am sure there are examples out there that check the actively selected item to hide/show commands based on properties of said item.

    Ryan

    Thursday, August 25, 2011 6:03 PM
  • Wrote these 5 years ago. It's an oldie, but a goodie.

       Using EnableVSIPLogging to identify menus and commands with VS 2005+SP1. (valid for VS 2008 and VS 2010
          though you need to change the version from 8.0 to 9.0 or 10.0 respectively in the registry key mentioned in
          the article)

        Using IVProfferCommands to retrieve a Visual Studio CommandBar

    These should make things substantially easier if you go the addin route.

    Sincerely,


    Ed Dore
    Saturday, August 27, 2011 5:47 AM

All replies

  • Using DTE and an AddIn vs VSCT and a package comes with tradeoffs, specifically perf is worse with DTE.  If that is fine in your situation then doing what you propose via DTE really isn't terribly different than what the samples you have read show.  The main differences would be

    1:  You need to fetch the proper context menu instead of MenuBar (which is the main menu) from the DTE.CommandBars collection.

    2:  As for dynamic visibility you need to implement IDteCommandTarget on your AddIn and handle the call back for the status of your menu, you likely would need to use IVsMonitorSelection or some DTE equivalent to fetch the active hierarchy and test if it is a XAML file.

    I don't have an example that does exactly what you want, but modifying an existing one to do the first part (place the command on the right menu) should be fairly simple, the second one is trickier but I am sure there are examples out there that check the actively selected item to hide/show commands based on properties of said item.

    Ryan

    Thursday, August 25, 2011 6:03 PM
  • Thank you for your reply.

    Performance is not an issue for me. However I was not able to find those examples that you mention, plus when you say I need to find a proper command, again in principles it's all easy, in practice, there are 450+ commands returning from _applicationObject.CommandBars, and two that made most sense for me ('Solution' and 'Solution Folder') are not he ones. 

    The reason why I was hoping someone could show me a code example is because I have exhausted myself searching. 

    Ryan, I understand things in principle, if you could kindly point me to the right blog/forum post/sample, that would be very much appreciated

    Rodion

    Friday, August 26, 2011 12:22 AM
  • There is some discussion of this here, also doing a web search for something like "DTE + solution explorer context menu" yields a number of results.  There are a lot of 'command bars' in VS (450+ as you mentioned).  This collection contains the main menu + all toolbars in the product + all context menus in the product, so there are a lot of them.  There are over 4,000 commands in VS and 11,000 placements of those commands (i.e. individual buttons on various menus/toolbars/etc).

    Ryan

    Friday, August 26, 2011 1:31 AM
  • Wrote these 5 years ago. It's an oldie, but a goodie.

       Using EnableVSIPLogging to identify menus and commands with VS 2005+SP1. (valid for VS 2008 and VS 2010
          though you need to change the version from 8.0 to 9.0 or 10.0 respectively in the registry key mentioned in
          the article)

        Using IVProfferCommands to retrieve a Visual Studio CommandBar

    These should make things substantially easier if you go the addin route.

    Sincerely,


    Ed Dore
    Saturday, August 27, 2011 5:47 AM