Excel功能区下拉菜单背景颜色
问题描述:
我已经创建了一个Excel功能区,其中有一个用户可以切换到我们插件的不同环境的下拉菜单,有没有一种方法可以在下拉菜单中为选定的值提供背景颜色,所以说生活的情况下,我可以显示带有红色背景的直播,同样带有绿色背景的开发Excel功能区下拉菜单背景颜色
答
无法更改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();
}
}
}
感谢财长的暗示这一点,它的一个很好的解决方法 –