hiding a button from a plugin toolbar

我怕爱的太早我们不能终老 提交于 2019-12-11 15:12:48

问题


I am developping a plugin for Visual Studio in C#. The plugin has a settings page and a few buttons in a toolbar to call commands.

The problem is that in some circonstances, I want to hide a specific button. The best I was able to do is to disable the button.

Is it possible to dynamically change it's visibility?

EDIT: I wrote this question from my mobile, so maybe there is not enough details...

I create the toolbar in the .vsct file (I created the menu in the same file)

<Button guid="guidProductCmdSet" id="startCommand" priority="0x0100" type="Button">
    <Parent guid="guidProductCmdSet" id="ToolbarGroup1" />
        <Icon guid="Icons" id="startIcon" />
        <Strings>
          <CommandName>startCommand</CommandName>
          <ButtonText>Start</ButtonText>
        </Strings>
</Button>

When the extension initializes, I create the commands:

var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (null != mcs)
{
  _startCommandId = new CommandID(GuidList.guidProducyVSICmdSet, (int)pkgCmdIDList.startCommand);
  var startItem = new MenuCommand(StartProcess, _startCommandId);
  mcs.AddCommand(startItem);
}

After, I am able to disable some buttons from the toolbar like this:

var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
var mc = mcs.FindCommand(commandId);
if (mc != null)
{
    mc.Enabled = false;
}

I tried mc.Visible = false, but it does nothing.


回答1:


Apparently, this works...

var commandBars = ((CommandBars)_dte2.CommandBars);
if (commandBars == null)
{
    return;
}
var commandBar = commandBars["MyPluginProductName"];
if (commandBar == null)
{
    return;
}
var startButton = commandBar.Controls["startCommand"];
if (startButton == null)
{
    return;
}
startButton.Visible = false;



回答2:


Add

<CommandFlag>AllowVisibilityChangeOnToolBar</CommandFlag>

to your button.

Best Regards,

Konstantin S.



来源:https://stackoverflow.com/questions/14572226/hiding-a-button-from-a-plugin-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!