简体   繁体   English

通过按钮以编程方式填充功能区

[英]Programmatically populate ribbon, with Button

Creating custom "Send on behalf of" button for shared mail box I would like to create the RibbonGroup and RibbonButton and add them to the RibbonTab programmatically. 为共享邮箱创建自定义的“代表发送”按钮我想创建RibbonGroupRibbonButton并将它们以编程方式添加到RibbonTab

My issue is that in CreateButtons() , adding button to RibbonGroup and RibbonTab will cause an error "Collection is read-only" . 我的问题是,在CreateButtons() ,将按钮添加到RibbonGroupRibbonTab会导致错误“集合为只读” Even if those are use in the designer as well. 即使这些也用于设计器中。

RibbonGroupReply.Items.Add(tempButton);
RibbonGroupNew.Items.Add(tempButton);
this.tab_MainComplement.Groups.Add(RibbonGroupNew);

I also try to use other method that were in the designer, I can now add in the RibbonGroup but not in the RibbonTab : 我还尝试使用设计器中的其他方法,现在可以在RibbonGroup添加,但不能在RibbonTab

tab_MainComplement.SuspendLayout();
RibbonGroupReply.SuspendLayout();
this.SuspendLayout();

I see no way out as dropping the tab for a new one will raise the same error on this.Tabs.Add(New_Tab); 我认为没有办法,因为删除新标签会在this.Tabs.Add(New_Tab);上引发相同的错误this.Tabs.Add(New_Tab); and adding the CreateButtons method inside the designer InitializeComponent break the layout and do not give better result. 在设计器的InitializeComponent内部添加CreateButtons方法会破坏布局,并且不会产生更好的结果。

Code : 代码:

public partial class BtnSender
{
    internal List<ButtonInfo> Buttons;

    private void BtnSender_Load(object sender, RibbonUIEventArgs e)
    {
        LoadButtonsList();
        CreateButtons();
    }

    private void CreateButtons()
    {
        //CreateNew Group           
        var buttonsNew = Buttons.Where(x => (x.Type & ButtonType.New) == 0);
        if (buttonsNew.Any())
        {
            OutlookRibbon.RibbonGroup RibbonGroupNew = this.Factory.CreateRibbonGroup();

            RibbonGroupNew.Label = "Nouveau Message";
            RibbonGroupNew.Name = "Nouveau Message";

            foreach (var butt in buttonsNew)
            {
                var tempButton = this.Factory.CreateRibbonButton();
                tempButton.ControlSize = RibbonControlSize.RibbonControlSizeLarge;
                tempButton.Image = global::CustomExpeditor.Properties.Resources.basic_mail;
                tempButton.Label = butt.Label;
                tempButton.Name = butt.Name + butt.Label.Replace(" ", string.Empty) + "New";
                tempButton.Description = butt.Address;
                tempButton.ShowImage = true;
                tempButton.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.Btn_SenderSI_Click);
                RibbonGroupNew.Items.Add(tempButton);
            }
            this.tab_MainComplement.Groups.Add(RibbonGroupNew);
        }

        //CreateReply Group
        var buttonsReply = Buttons.Where(x => (x.Type & ButtonType.Reply) == ButtonType.Reply);
        if (buttonsReply.Any())
        {
            OutlookRibbon.RibbonGroup RibbonGroupReply = this.Factory.CreateRibbonGroup();
            //tab_MainComplement.SuspendLayout();
            //RibbonGroupReply.SuspendLayout();
            //this.SuspendLayout();

            RibbonGroupReply.Label = "Répondre à";
            RibbonGroupReply.Name = "Répondre à";

            foreach (var butt in buttonsNew)
            {
                var tempButton = this.Factory.CreateRibbonButton();
                tempButton.ControlSize = RibbonControlSize.RibbonControlSizeLarge;
                tempButton.Image = global::CustomExpeditor.Properties.Resources.basic_mail;
                tempButton.Label = butt.Label;
                tempButton.Name = butt.Name + butt.Label.Replace(" ", string.Empty) + "Reply";
                tempButton.Description = butt.Address;
                tempButton.ShowImage = true;
                tempButton.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(Btn_ResponseSI_Click);
                RibbonGroupReply.Items.Add(tempButton);
            }
            tab_MainComplement.Groups.Add(RibbonGroupReply);
        }
    }

    private void LoadButtonsList()
    {
        // Will evolve to a more configurable list in the future. 
        Buttons = new[] {
            new ButtonInfo{ Label="Mail Test", Address="MailTest@domain.not", Type=ButtonType.New & ButtonType.Reply },
            new ButtonInfo{ Label="Serv Info", Address="MailTest@domain.not", Type=ButtonType.New & ButtonType.Reply  },
            new ButtonInfo{ Label="Serv Log", Address="MailTest@domain.not", Type=ButtonType.New & ButtonType.Reply  },
            new ButtonInfo{ Label="Titi", Address="MailTest@domain.not", Type=ButtonType.New  }
        }.ToList();
    }
}

public class ButtonInfo
{
    public string Name, Label, Address;
    public ButtonType Type;
}
[Flags] public enum ButtonType { New = 1, Reply = 2 };

Those buttons are read-only right after initialization, just like the groups, tabs, etc. Adding them dynamically after initialization doesn't work. 这些按钮在初始化后即是只读的,就像组,标签等一样。初始化后动态添加它们是行不通的。

I have solved this a few times by adding buttons up-front, and dynamically fill them with the correct labels. 我已经通过在前面添加按钮,并用正确的标签动态填充按钮来解决了几次。 Some control types do allow dynamic buttons, like a ribbon gallery. 某些控件类型确实允许使用动态按钮,例如功能区库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM