How to enable a new custom Context Menu Item in Visual Studio Solution Explorer?

834 Views Asked by At

I would like to have "File.CopyRelativePath" as an option in the Context Menu of Solution Explorer when I right click a file.

I am using Visual Studio 2022

I have managed to put the command in the menu by adding it at: Tools - Customize - Commands - Context menu - Project and Solution Context Menus | Item

enter image description here

There I have added File.CopyRelativePath and given it a shortcut too.

enter image description here

But when I see it in the context menu it is disabled.

My new custom Context Menu item is disabled :(

How do I enable the menu item so I can start using it?

1

There are 1 best solutions below

2
On

TL;DR: You can't force the command handler to be enabled. However, you could implement your own handler for this command (or a brand new command with the same label).

More details:

There's a lot of documentation about how commands work in VS, but to quickly paraphrase: command handlers implement an Execute method to do the operation and optionally a QueryStatus method to determine if the command is currently able to execute. In this case, QueryStatus is determining that the command should not be enabled.

The implementation of this command's QueryStatus (I peeked) looks at the currently active WindowFrame, and requires that it be associated with an open document. Once the focus is in Solution Explorer, it would determine that the command should not be enabled as Solution Explorer is not a document window.

(The implementation, similarly, uses properties of the frame that are only set for a document instance. It wouldn't understand an input from another type of window.)