Программно заполнить ленту кнопкой - PullRequest
0 голосов
/ 30 октября 2018

Создание пользовательской кнопки «Отправить от имени» для общего почтового ящика. Я хотел бы создать RibbonGroup и RibbonButton и добавить их в RibbonTab программным способом.

Моя проблема заключается в том, что в CreateButtons() добавление кнопки к RibbonGroup и RibbonTab приведет к ошибке «Коллекция доступна только для чтения» . Даже если они используются в дизайнере.

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

Я также пытаюсь использовать другой метод, который был в конструкторе, теперь я могу добавить в RibbonGroup, но не в RibbonTab:

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

Я не вижу выхода, так как удаление новой вкладки вызовет ту же ошибку на this.Tabs.Add(New_Tab); и добавление метода CreateButtons в конструкторе InitializeComponent нарушают макет и не дают лучшего результата.

код:

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 };

1 Ответ

0 голосов
/ 30 октября 2018

Эти кнопки доступны только для чтения сразу после инициализации, так же, как группы, вкладки и т. Д. Добавление их динамически после инициализации не работает.

Я решил эту проблему несколько раз, добавив кнопки вперед и динамически заполняя их правильными метками. Некоторые типы элементов управления допускают динамические кнопки, например, галерею ленты.

...