以编程方式启用/禁用加载项按钮(适用于 Microsoft VSTO C# 加载项)

问题描述 投票:0回答:1

我正在制作一个 PowerPoint 插件。某些按钮仅应在用户选择表格时启用,因为这些功能仅适用于表格。我无法让它发挥作用。看起来一切正常,我可以设置 button.Enabled = true 但 UI 没有更新。我能做什么?

这是代码的简化版本。为了简单起见,我删除了逻辑“如果选择==表,则启用。否则,禁用”。现在,我只想更新 UI

类ThisAddIn.cs

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
namespace PPT_CS_ADDIN
{
    public partial class ThisAddIn
    {
        private UserControl1 myUserControl;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            myUserControl = new UserControl1();
            this.Application.WindowSelectionChange += Application_WindowSelectionChange;
        }
        private void Application_WindowSelectionChange(PowerPoint.Selection Sel)
        {
            myUserControl.UpdateButtonStates(Sel);
        }
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }       
    }
}

类UserControl1.cs

using Microsoft.Office.Tools.Ribbon;
namespace PPT_CS_ADDIN
{
    public partial class UserControl1
    {
        
        private void UserControl1_Load(object sender, RibbonUIEventArgs e)
        {
        }

        public void UpdateButtonStates(Microsoft.Office.Interop.PowerPoint.Selection selection)
        {
            button11.Enabled = false;
        }
    }
}

类UserControl1.Designer.cs

namespace PPT_CS_ADDIN
{
    partial class UserControl1 : Microsoft.Office.Tools.Ribbon.RibbonBase
    {

        private System.ComponentModel.IContainer components = null;

        public UserControl1()
            : base(Globals.Factory.GetRibbonFactory())
        {
            InitializeComponent();

        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.tab1 = this.Factory.CreateRibbonTab();
            this.group3 = this.Factory.CreateRibbonGroup();
            this.button11 = this.Factory.CreateRibbonButton();
            this.button13 = this.Factory.CreateRibbonButton();
            this.tab1.SuspendLayout();
            this.group3.SuspendLayout();
            this.SuspendLayout();
            // tab1
            this.tab1.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office;
            this.tab1.Groups.Add(this.group3);
            this.tab1.Label = "TabAddIns";
            this.tab1.Name = "tab1";
            // group3
            this.group3.Items.Add(this.button11);
            this.group3.Items.Add(this.button13);
            this.group3.Label = "Table";
            this.group3.Name = "group3";
            // button11
            this.button11.Label = "Button";
            this.button11.Name = "button11";
            // UserControl1
            this.Name = "UserControl1";
            this.RibbonType = "Microsoft.PowerPoint.Presentation";
            this.Tabs.Add(this.tab1);
            this.Load += new Microsoft.Office.Tools.Ribbon.RibbonUIEventHandler(this.UserControl1_Load);
            this.tab1.ResumeLayout(false);
            this.tab1.PerformLayout();
            this.group3.ResumeLayout(false);
            this.group3.PerformLayout();
            this.ResumeLayout(false);
        }
        internal Microsoft.Office.Tools.Ribbon.RibbonTab tab1;
        internal Microsoft.Office.Tools.Ribbon.RibbonGroup group3;
        internal Microsoft.Office.Tools.Ribbon.RibbonButton button11;
        internal Microsoft.Office.Tools.Ribbon.RibbonButton button13;
    }

    partial class ThisRibbonCollection
    {
        internal UserControl1 UserControl1
        {
            get { return this.GetRibbon<UserControl1>(); }
        }
    }
}

我调试了整个流程,看来问题是button11没有刷新。我在按钮和 UserControl 上尝试了 Invalidate、ResumeLayout、SuspendLayout、PerformDynamicLayout、Refresh 方法,但它不起作用。

c# user-controls vsto office-addins
1个回答
0
投票

我在 GitHub 这里有一个示例项目

C#代码

您可以拨打电话

    ribbon.InvalidateControl(control.Id);

和功能

    public bool GetEnabledProcedureName(Office.IRibbonControl control)
    {
        try
        {
            switch (control.Id)
            {
                case "btnYourButton":
                case "btnCleanData":
                case "btnZeroToNull":
                case "btnFormatDateColumns":
                case "btnFormatTimeColumns":
                case "btnClearInteriorColor":
                case "btnAddScriptColumn":
                    return ErrorHandler.IsEnabled(false);
                default:
                    return false;
            }
        }
        catch (Exception ex)
        {
            ErrorHandler.DisplayMessage(ex);
            return false;
        }
    }

功能区 XML

                <button id="btnYourButton"
                        label="Your Button Label"
                        onAction="OnActionProcedureName"
                        getEnabled="GetEnabledProcedureName"
                        imageMso="ImageReferenceNameHere"
                        size="normal"
                        screentip="Your Screen Tip"
                        supertip="Your super tip"
                        keytip="CVC"/>
© www.soinside.com 2019 - 2024. All rights reserved.