Office Ribbon: How to access a control when the ri

2019-06-06 10:07发布

问题:

I used a ribbon XML file to create a custom tab which contains a togglebutton. The button is meant to toggle the visibility of a custom task pane and works great. The problem is that when the user close the custom task pane, the toggle button is now out of sync. How do I programmaticly access the togglebutton so I can change its IsChecked value?

回答1:

You need to handle the VisibleChanged event. Add the following method to your ThisAddIn class - when the user closes the task pane by clicking the Close button (X), this method updates the state of the toggle button on the Ribbon.

private void taskPaneValue_VisibleChanged(object sender, System.EventArgs e)
{
    Globals.Ribbons.ManageTaskPaneRibbon.toggleButton1.Checked = 
        taskPaneValue.Visible;
}

(see more info on this in Walkthrough: Synchronizing a Custom Task Pane with a Ribbon Button)