为共享邮箱创建自定义的“代表发送”按钮,我想创建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 };
答案 0 :(得分:1)
这些按钮在初始化后即是只读的,就像组,选项卡等。初始化后动态添加它们是行不通的。
我已经解决了几次,方法是在前面添加按钮,并用正确的标签动态填充按钮。某些控件类型确实允许使用动态按钮,例如功能区库。