动态更改功能区标签

发布于 2024-12-01 14:16:42 字数 172 浏览 1 评论 0原文

我已经为 Outlook 2010 加载项创建了 VSTO 功能区。当我以前使用设计器时,我能够动态更改功能区按钮的标签。我现在正在手动编码(XML/C#),并且似乎无法确定如何完成同样的事情。 XML 中的“标签”项似乎是静态的。

仅供参考 - 其背后的目的是为用户识别图库中的项目数量。

谢谢。

I've created a VSTO Ribbon for an Outlook 2010 add-in. When I previously used the designer, I was able to dynamically change the label of the ribbon button. I'm now coding this by hand (XML/C#) and can't seem to determine how to accomplish the same thing. The "label" item in the XML seems to be static.

FYI - the purpose behind this is to identify the number of items in the gallery for the user.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上得阳光 2024-12-08 14:16:42

您可以在元素上设置一个 getLabel 属性。该值是回调函数的名称,调用该函数以动态提供标签名称。您可以通过编程方式刷新 UI 以强制调用所有回调。

There is a getLabel attribute you can set on your element. The value is the name of a callback function which is called to provide the label name dynamically. You can programmatically refresh the UI to force all of your callbacks to be called.

流殇 2024-12-08 14:16:42

这应该适用于使用 C# 的 excel 和 Outlook。

我将根据您的问题假设您可以创建大部分 xml 来将按钮放到功能区上,因此我将跳过该部分。您也没有提到什么样的事件应该更改标签按钮,因此我假设单击按钮将导致标签文本更改。

首先将按钮 xml 代码放置到位

<button id="YourUniqueId" onAction="YourUniqueId_Click" getLabel="GetYourLabelText">

创建一个类级别变量,功能区类中的所有方法都可以在构造函数之前访问该变量,然后在构造函数中设置该值。您还需要功能区 UI 类变量。当使用 xml 中的加载方法(例如 onLoad="Ribbon_Load")加载功能区时,应实例化此变量。

public class Ribbon: Office.IRibbonExtensibility
{
    private bool _buttonClicked;
    private Office.RibbonUI ribbon;

    public Ribbon()
    {
       _buttonClicked = false;
    }

    public void Ribbon_Load(Office.IRibbonUI ribbonUI)
    {
        ribbon = ribbonUI;
    }
}

接下来,在功能区类中,您将需要两个类“YourUniqueId_Click”和“GetYourLabelText”。

public void YourUniqueId_Click(Office.IRibbonControl Control)
{
    //Since the initial value is false and presumably the user just clicked for 
    //the first (or N-th) time you'll want to set the value to true
    if(!_buttonClicked)
    {
        _buttonClicked = true;
    }
    //Or if clicking for a second (or N-th + 1) time, set the value to false
    else
    {
        _buttonClicked = false;
    }

    //Now use the invalidate method from the ribbon variable (from the load method) 
    //to reset the specific control id (in this case "YourUniqueId") from the xml. 
    //Invalidating the control will call the method "GetYourLabelText"
    ribbon.InvalidateControl(Control.Id);      
}

public string GetYourLabelText(Office.IRibbonUI Control)
{
    if(_buttonClicked)
    {
        return "Button is On";
    }
    else
    {
        return "Button is Off";
    }
}

当功能区最初加载到 Outlook 或 Excel 中时,“GetYourLabelText”方法将运行。由于类变量“_buttonClicked”在构造函数中设置为 false,因此按钮标签将以“Button is Off”开始。每次单击按钮时,“_buttonClicked”都会更改布尔状态,然后按钮会重置,再次调用“GetYourLabelText”方法。

This should work for excel and outlook using C#.

I'm going to assume based on your question that you can create the majority of the xml to get the button onto the ribbon, so I'll skip that part. You also didn't mention what kind of an event should change the label button, so I'm going to assume that clicking the button will cause the label text change.

First get the button xml code into place

<button id="YourUniqueId" onAction="YourUniqueId_Click" getLabel="GetYourLabelText">

Create a class level variable that will be accessible to all the methods in your ribbon class before the constructor, and then set the value in your constructor. You'll also need the ribbon UI class variable. This variable should be instantiated when the ribbon loads using a load method in the xml such as onLoad="Ribbon_Load".

public class Ribbon: Office.IRibbonExtensibility
{
    private bool _buttonClicked;
    private Office.RibbonUI ribbon;

    public Ribbon()
    {
       _buttonClicked = false;
    }

    public void Ribbon_Load(Office.IRibbonUI ribbonUI)
    {
        ribbon = ribbonUI;
    }
}

Next in your ribbon class you'll need two classes "YourUniqueId_Click" and "GetYourLabelText".

public void YourUniqueId_Click(Office.IRibbonControl Control)
{
    //Since the initial value is false and presumably the user just clicked for 
    //the first (or N-th) time you'll want to set the value to true
    if(!_buttonClicked)
    {
        _buttonClicked = true;
    }
    //Or if clicking for a second (or N-th + 1) time, set the value to false
    else
    {
        _buttonClicked = false;
    }

    //Now use the invalidate method from the ribbon variable (from the load method) 
    //to reset the specific control id (in this case "YourUniqueId") from the xml. 
    //Invalidating the control will call the method "GetYourLabelText"
    ribbon.InvalidateControl(Control.Id);      
}

public string GetYourLabelText(Office.IRibbonUI Control)
{
    if(_buttonClicked)
    {
        return "Button is On";
    }
    else
    {
        return "Button is Off";
    }
}

The "GetYourLabelText" method will run when the ribbon is initially loaded in outlook or excel. Since the class variable "_buttonClicked" is set to false in the constructor, the button label will start off as "Button is Off". Each time the button is clicked the "_buttonClicked" changes boolean state then the button resets calling the "GetYourLabelText" method again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文