Excel功能区下拉菜单背景颜色 [英] Excel Ribbon Dropdown background color
问题描述
我创建了一个Excel功能区,其中有一个下拉菜单供用户切换到我们插件的不同环境,是否可以通过这种方法将下拉菜单中的选定背景值赋予背景颜色,因此在实时情况下我可以同样以红色背景显示Live,以绿色背景显示Dev
I have created a Excel Ribbon where am having a dropdown for the user to switch to differenct Environments of our plugin, is there a way by which I can give background color to the selected value in dropdown, so say in case of live I can show Live with red background, Dev with green background likewise
推荐答案
无法更改DropDown(或项目)的背景,但是您可以为每个项目使用不同的图像.像这样:
It's not possible to change the background of the DropDown (or the items), but you can use different images for each item. Something like this:
<?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>
不幸的是,图像的用户选择后不可见,从下拉的项,所以你必须无效色带和动态地设置到控制新的图像,当选择的变化.
Unfortunately the image is not visible after the user selects an item from the dropdown, so you have to invalidate the ribbon and dynamically set a new image to the control, when the selection changes.
类似这样的东西:
[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();
}
}
}
这篇关于Excel功能区下拉菜单背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!