我创建了一个Excel功能区,其中有一个下拉列表供用户切换到我们的插件的不同环境,有没有办法让我可以在下拉列表中为所选值提供背景颜色,所以说如果是直播我可以用红色背景显示Live,同样用绿色背景显示Dev
答案 0 :(得分:3)
无法更改DropDown(或项目)的背景,但您可以为每个项目使用不同的图像。像这样:
<?xml version="1.0" encoding="utf-8" ?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
<ribbon>
<tabs>
<tab id="MyAddInTab" label="MY ADD-IN TAB">
<group id="EnvironmentGroup" label="Environment">
<dropDown id="environmentDropDown" showImage="true" showItemImage="true"
getImage="OnEnvironmentGetImage"
onAction="OnEnvironmentSelectionChanged"
getSelectedItemID="OnEnvironmentGetSelectedItemId">
<item id="environmentDev" imageMso="ColorGreen" label="Development" />
<item id="environmentTest" imageMso="ColorYellow" label="User Testing" />
<item id="environmentProd" imageMso="ColorRed" label="Production" />
</dropDown>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
不幸的是,在用户从下拉列表中选择项目后,图像不可见,因此当选择更改时,您必须使功能区无效并动态地将新图像设置到控件。
这样的事情:
[ComVisible(true)]
public class MyAddInRibbon : ExcelRibbon
{
private IRibbonUI _thisRibbon;
private string _selectedEnvironmentId = "environmentDev"; // Defaults to Dev
public void OnLoad(IRibbonUI ribbon)
{
if (ribbon == null)
{
throw new ArgumentNullException(nameof(ribbon));
}
_thisRibbon = ribbon;
}
public string OnEnvironmentGetSelectedItemId(IRibbonControl control)
{
return _selectedEnvironmentId;
}
public void OnEnvironmentSelectionChanged(IRibbonControl control,
string selectedId, int selectedIndex)
{
_selectedEnvironmentId = selectedId;
// Invalidate the drop down, so we can update the image next to the dropdown
_thisRibbon.InvalidateControl("environmentDropDown");
}
public string OnEnvironmentGetImage(IRibbonControl control)
{
// This displays the image next to the dropdown
switch (_selectedEnvironmentId)
{
case "environmentDev":
return "ColorGreen";
case "environmentTest":
return "ColorYellow";
case "environmentProd":
return "ColorRed";
default:
throw new InvalidOperationException();
}
}
}