代码优先:
以编程方式向按钮栏添加按钮:
for (int i = 0; i < titles.Length; i++)
{
RibbonButton button = this.Factory.CreateRibbonButton();
button.Visible = false;
button.Label = titles[i];
button.Image = OutlookAddIn1.Properties.Resources.Sans_titre_5;
button.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
this.group1.Items.Add(button);
}
以编程方式显示/隐藏某些按钮:
private void showOrHide(contact){
// Building a phone array with the contact infos...
RibbonButton button = Globals.Ribbons.Ribbon1.ribbonButtons.ElementAt(i).Value;
button.Visible = button.Enabled = phones[i] != null;
}
我在所有contactItems.open上绑定了一个事件,并调用此方法:
private void Event(ref bool asd)
{
Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
if (selection.OfType<Outlook.ContactItem>().Count() == 1)
{
Outlook.ContactItem contact = selection.OfType<Outlook.ContactItem>().FirstOrDefault();
showOrHide(contact);
}
}
您是否可以看到,我正在尝试显示/隐藏功能区中的按钮,具体取决于联系人是否具有某种电话号码类型。
我第一次打开联系人时,正确显示/隐藏了带状按钮:
但是当我选择另一个联系人(或相同的)时,它会显示我的所有按钮,没有标签图像,即使它们是可见的= false:
我的猜测是,当我第一次关闭联系人窗口时,Outlook会破坏我的带状按钮。因此,当我打开另一个时,带状按钮全都搞乱了。 有人有想法吗?
答案 0 :(得分:-1)
使用this方法为加载项添加功能区控件。它相当可靠。
将Visual Studio中的功能区xml添加到您的解决方案中,然后您可以将控件添加到功能区中,如下所示。
<tab idMso="TabAddIns">
<group id="ContentGroup" label="Content">
<button id="textButton" label="Insert Text"
screentip="Text" onAction="OnTextButton"
supertip="Inserts text at the cursor location."/>
<button id="tableButton" label="Insert Table"
screentip="Table" onAction="OnTableButton"
supertip="Inserts a table at the cursor location."/>
</group>
</tab>
您可以为事件定义回调,并在运行时为它们声明标签和其他属性。您可以通过声明控件的“getVisible”属性的回调方法来使功能区控件无效并控制其可见性。