RibbonX 动态生成ComboBox和DropDown的Item
2011-12-15 13:13
585 查看
先让我们来看一下xml文件的设定:
这个xml文件设定了一个ID是ComboBoxSample的ComboBox和一个DropDownSample的DropDown.并且为这两个控件设定了一些内置调用方法(这是我自己给起的说法,因为这些方法不是被控件以外的程序调用,而是控件自身调用的)。由于ComboBox和DropDown本来很像所以其内置方法也一样。其中getItemCount内置方法是其他内置方法实现的先决条件。没有这个方法其他内置方法将会出错。
另外getItemID内置方法用处不大,致少在UI显示上我们不能看到它起作用(Debug时你会发现此方法会先于getItemLabel被调用(调用顺序getItemCount->getItemID->getItemLabel->getItemScreenTip)。
下面是Ribbon1.cs
相关资源:http://download.csdn.net/detail/tx_officedev/3923959
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load"> <ribbon> <tabs> <tab id="TabCustom" label="ExcelAddIn12Tab"> <group id="GroupExcelAddIn12" label="ExcelAddIn12"> <comboBox id="ComboBoxSample" getItemCount ="ComboBoxGetItemCount" getItemLabel ="ComboBoxGetItemLabel" getItemID="ComboBoxGetItemID" getItemScreentip="ComboBoxGetItemScreenTip"/> <dropDown id="DropDownSample" getItemCount="DropDownGetItemCount" getItemLabel ="DropDownGetItemLabel" getItemID ="DropDownGetItemID" getItemScreentip ="DropDowGetItemScreenTip"/> </group> </tab> </tabs> </ribbon> </customUI>
这个xml文件设定了一个ID是ComboBoxSample的ComboBox和一个DropDownSample的DropDown.并且为这两个控件设定了一些内置调用方法(这是我自己给起的说法,因为这些方法不是被控件以外的程序调用,而是控件自身调用的)。由于ComboBox和DropDown本来很像所以其内置方法也一样。其中getItemCount内置方法是其他内置方法实现的先决条件。没有这个方法其他内置方法将会出错。
另外getItemID内置方法用处不大,致少在UI显示上我们不能看到它起作用(Debug时你会发现此方法会先于getItemLabel被调用(调用顺序getItemCount->getItemID->getItemLabel->getItemScreenTip)。
下面是Ribbon1.cs
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using Office = Microsoft.Office.Core; // TODO: Follow these steps to enable the Ribbon (XML) item: // 1: Copy the following code block into the ThisAddin, ThisWorkbook, or // ThisDocument class. // protected override Microsoft.Office.Core.IRibbonExtensibility //CreateRibbonExtensibilityObject() // { // return new Ribbon1(); // } // 2. Create callback methods in the "Ribbon Callbacks" region of this class to // handle user actions, such as clicking a button. Note: if you have exported // this Ribbon from the Ribbon designer, move your code from the event handlers // to the callback methods and modify the code to work with the Ribbon // extensibility (RibbonX) programming model. // 3. Assign attributes to the control tags in the Ribbon XML file to identify // the appropriate callback methods in your code. // For more information, see the Ribbon XML documentation in the Visual Studio // Tools for Office Help. namespace ExcelAddIn12 { [ComVisible(true)] public class Ribbon1 : Office.IRibbonExtensibility { private Office.IRibbonUI ribbon; public Ribbon1() { } #region IRibbonExtensibility Members public string GetCustomUI(string ribbonID) { return GetResourceText("ExcelAddIn12.Ribbon1.xml"); } #endregion #region Ribbon Callbacks //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1 public void Ribbon_Load(Office.IRibbonUI ribbonUI) { this.ribbon = ribbonUI; } #endregion #region Helpers private static string GetResourceText(string resourceName) { Assembly asm = Assembly.GetExecutingAssembly(); string[] resourceNames = asm.GetManifestResourceNames(); for (int i = 0; i < resourceNames.Length; ++i) { if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0) { using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i]))) { if (resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } #endregion #region MyCode public int ComboBoxGetItemCount(Office.IRibbonControl Control) { return 5; } public string ComboBoxGetItemLabel(Office.IRibbonControl Control, int Index) { return "ComboBoxItem" + Index.ToString(); } public string ComboBoxGetItemID(Office.IRibbonControl Control, int Index) { return "cb" + Index.ToString(); } public int DropDownGetItemCount(Office.IRibbonControl Control) { return 3; } public string DropDownGetItemLabel(Office.IRibbonControl Control, int Index) { return "DropDownItem" + Index.ToString(); } public string DropDownGetItemID(Office.IRibbonControl Control, int Index) { return "dd" + Index.ToString(); } public string ComboBoxGetItemScreenTip(Office.IRibbonControl Control, int Index) { return string.Format("This is \"{0}\", its id is {1}", "ComboBoxItem" + Index.ToString(), "cb" + Index.ToString()); } public string DropDowGetItemScreenTip(Office.IRibbonControl Control, int Index) { return string.Format("This is \"{0}\", its id is {1}", "DropDownItem" + Index.ToString(), "dd" + Index.ToString()); } #endregion } }
相关资源:http://download.csdn.net/detail/tx_officedev/3923959
相关文章推荐
- RibbonX 动态生成ComboBox和DropDown的Item
- C#如何遍历动态生成的textbox,Combobox 等控件
- Qt tableview 使用ComboBox作为delegate时,实现单个cell中的comboBox的item动态变化以及独特性
- 自定义控件---动态无限级生成 级联comboBox 和 treeView
- GridView中动态生成ItemTemplate模板列
- Qt程序学习(三)------QTreeWidget、右键菜单、动态改变comboBox、QTreeWidgetItem的对应列的文字编辑
- C# 动态生成textbox,Combobox等控件
- 为DataGridview的Combobox列动态生成下拉列表
- easyui根据树数据动态生成多个combobox
- Listiew动态生成Item时只显示最新生成的这条item
- 【delphi】 TComboBox 动态生成下拉,并展开后鼠标丢失问题
- easyui生成动态关联的combobox
- WPF开发中遇到的问题及解决系列(二):如何获取通过binding生成的ComboBox或者ListBox等的Item
- 动态生成控件-绑定事件(VB.NET/C#)
- WEB BUILDER中的动态页面生成
- 学习 gcc编译器使用3 生成动态链接
- MySQL中动态生成多条件查询语句
- js动态生成css代码
- 动态生成年月日
- 解决动态生成input无法触发click事件的问题