首页 > 解决方案 > 如何从功能区 ExcelDNA 中检索控件

问题描述

我想知道您如何能够从 ExcelDNA 生成的 .dna 文件中定义的自定义 RibbonUI 中检索控件?目前我正在实施一个登录工作流程,其中一个按钮被禁用。用户将在哪里登录,如果成功将启用禁用按钮。

标签: c#.netexcelcomexcel-dna

解决方案


Office 功能区界面完全基于回调。因此,您实际上无法从功能区“检索控件”并在这些控件上设置属性。您通过触发回调来实现状态更改 - 在这种情况下可能是您的getEnabled回调。要触发功能区的失效,以便再次评估您的回调,请在加载功能区时获得的功能区界面上调用 Invalidate。

一些实现这一点的 C# 代码可能如下所示:

public class MyRibbon: ExcelRibbon 
{ 
  private static IRibbonUI _ribbonUi; 

  // This ribbon xml can be returned in code, or places in the .dna     file 
  public override string GetCustomUI(string uiName) 
  { 
    return 
    @" 
    <customUI onLoad='Ribbon_Load' 
              xmlns='http://schemas.microsoft.com/office/2006/01/customui'> 
      <ribbon> 
        <tabs> 
          <tab idMso='TabAddIns'> 
            <group id='group1' label='Group1'> 
              <button id='button1' getEnabled='btn_GetEnabled' getLabel='btn_GetLabel' /> 
            </group> 
          </tab> 
        </tabs> 
      </ribbon> 
    </customUI> 
    "; 
  } 
  public void Ribbon_Load(IRibbonUI sender) 
  { 
    _ribbonUi = sender; 
  } 
  public bool btn_GetEnabled(IRibbonControl control) 
  { 
    return true; 
  } 
  public bool btn_GetLabel(IRibbonControl control) 
  { 
    return "My Button"; 
  } 
  public static void Refresh() 
  { 
    if (_ribbonUi != null) { _ribbonUi.Invalidate(); } 
  } 
} 

推荐阅读