Wrapper class RibbonItems - harborsiem/WindowsRibbon GitHub Wiki

Wrapper class RibbonItems for code behind to the Ribbon

The custom build tool of RibbonTools generates source files named RibbonItems.Designer.cs for the programming language C# or RibbonItems.Designer.vb for Visual Basic. In this file you get a wrapper class for all Ribbon items (Controls, ...) one defined in the RibbonMarkup.xml file. You should not modify this file. The wrapper class is defined as a partial class. So, you can extend this class by a file named RibbonItems.cs with the same namespace and class name. The namespace of the wrapper class is “RibbonLib.Controls” and the class name is RibbonItems. Don’t forget the partial keyword at the class declaration. The constructor of the wrapper class RibbonItems should be called in the Form constructor after InitializeComponent(). The parameter of the constructor is the Ribbon Control which is placed to the Form. In the file RibbonItems.cs you can define the whole logic including events for the Ribbon items. In your user defined RibbonItems.cs you can also define additional constructors with some more parameters if you need them in your logic. These constructors have to call the standard constructor in RibbonItems.Designer.cs with the Ribbon Control as parameter.

Example MainForm

...
using RibbonLib;
using RibbonLib.Controls;

namespace your_namespace
{    public partial class MainForm : Form
    {
        private RibbonItems _ribbonItems;

        public MainForm()
        {
            InitializeComponent();
            ribbon.RibbonEventException += Ribbon_RibbonEventException;
            _ribbonItems = new RibbonItems(ribbon);
            _ribbonItems.Init(this);
        }
     }
}

File: RibbonItems.cs

namespace RibbonLib.Controls
{
    partial class RibbonItems
    {

        /// <summary>
        /// Must be called from MainForm Constructor
        /// </summary>
        /// <param name="form">The MainForm</param>
        public void Init(MainForm form)
        {
        ...
        }

[!Hint] If you have more than one Ribbon Control for different Forms in the project, then you should name the RibbonMarkup.xml to RibbonMarkup1.xml, RibbonMarkup2.xml, ... RibbonMarkup9.xml because we got different classes of RibbonItems(x).Designer.cs . The build tool takes the last number of the xml filename for the RibbonItems file.

With optional setting switch "Wrapper class name like Markup file instead RibbonItems" one can generate a wrapper file and class name like the markup file. This will help for multiple Ribbon Controls in an application.

⚠️ **GitHub.com Fallback** ⚠️