steeveeet
steeveeet

Reputation: 668

Ribbon toggle button getPressed() not being called when expected

I've added a custom toggle button to the new email ribbon in outlook 2013. When the button is toggled on, it adds a user property to the mail message.

    public void OnLockButton(Office.IRibbonControl control, bool pressed)
    {
        Outlook.MailItem mi = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as Outlook.MailItem;
        var userProp = mi.UserProperties.Add("MyIsLocked", Outlook.OlUserPropertyType.olYesNo, false);
        userProp.Value = pressed;

        // Make sure we update the ribbon
        ribbon.Invalidate();
    }

The 'toggle' state of the button is being updated with a getPressed() callback that checks the state of the user property:

    public bool GetLockButtonPressed(Office.IRibbonControl control)
    {
        Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem as Outlook.MailItem;
        var userProp = mailItem.UserProperties.Find("NMIsLocked");

        bool isLocked = (userProp != null && userProp.Value);

        return isLocked;
    }

This is all working just fine.

The problem happens when:

  1. Open a new mail message
  2. Switch on the toggle button
  3. Discard the mail message
  4. Open a new mail message

At this point the toggle button looks like it's switched on on the new mail message even though the user property is not set on that message.

In the debugger I can see that getPressed() is not being called for the new message, so I think that the ribbon button state is just the same as the last time it was drawn.

Some ideas I've had include forcing the ribbon to be invalidated when the mail message is opened (or closed?) Or is there some other way that I've missed?

I'm looking for similar functionality to the 'High Priority' toggle button in Outlook.

Upvotes: 0

Views: 1832

Answers (1)

steeveeet
steeveeet

Reputation: 668

As indicated in this thread, you need a callback to invalidate the ribbon when an inspector is activated. You can add this event handler whenever a new inspector is created.

public class MyRibbon: Office.IRibbonExtensibility
{
    private Office.IRibbonUI ribbon;

    public void Ribbon_Load(Office.IRibbonUI ribbonUI)
    {
        this.ribbon = ribbonUI;
        // ensure that any new inspectors created have a callback to refresh the button state on ativation.
        Globals.ThisAddIn.Application.Inspectors.NewInspector += Inspectors_NewInspector;
    }

    void Inspectors_NewInspector(Outlook.Inspector Inspector)
    {
        ((Outlook.InspectorEvents_10_Event)Inspector).Activate += Inspector_Activate;
    }

    void Inspector_Activate()
    {
        ribbon.Invalidate();
    }
}

Upvotes: 2

Related Questions