使用 Button 以编程方式填充功能区
Programmatically populate ribbon, with Button
为共享邮箱创建自定义 "Send on behalf of" 按钮 我想创建 RibbonGroup
和 RibbonButton
并以编程方式将它们添加到 RibbonTab
。
我的问题是,在 CreateButtons()
中,将按钮添加到 RibbonGroup
和 RibbonTab
会导致错误 "Collection is read-only"。即使那些也在设计器中使用。
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 };
这些按钮在初始化后是只读的,就像组、选项卡等一样。初始化后动态添加它们不起作用。
我已经通过预先添加按钮并用正确的标签动态填充它们解决了几次这个问题。一些控件类型确实允许动态按钮,例如功能区库。
为共享邮箱创建自定义 "Send on behalf of" 按钮 我想创建 RibbonGroup
和 RibbonButton
并以编程方式将它们添加到 RibbonTab
。
我的问题是,在 CreateButtons()
中,将按钮添加到 RibbonGroup
和 RibbonTab
会导致错误 "Collection is read-only"。即使那些也在设计器中使用。
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 };
这些按钮在初始化后是只读的,就像组、选项卡等一样。初始化后动态添加它们不起作用。
我已经通过预先添加按钮并用正确的标签动态填充它们解决了几次这个问题。一些控件类型确实允许动态按钮,例如功能区库。