自定义 BindingNavigator 的 ToolStripButtons 在窗体设计器中显示为锁定

ToolStripButtons of a custom BindingNavigator appear locked in the Form Designer

我正在尝试使用一些额外的 ToolStripButton(EditExportToExcel)创建自定义 BindingNavigator 控件。
ToolStripButton 添加到 BindingNavigator,但我无法 select 这个新的 ToolStripButton,例如,在其 Click 事件处理程序中添加代码。实际上,ToolStripButtons 出现 locked.

这是我的代码和描述问题的图像:

using System.Windows.Forms;

public class BindingNavigator : System.Windows.Forms.BindingNavigator
{
    public ToolStripButton btnEdit;
    public ToolStripButton btnExcelExport;

    public BindingNavigator()
    {
        this.LayoutCompleted += BindingNavigator_LayoutCompleted;
    }

    public void BindingNavigator_LayoutCompleted(object sender, System.EventArgs e)
    {
        if (this.Items.Contains(btnEdit))
            return;
        if (this.Items.Count >= 11)
        {
            btnEdit = new ToolStripButton();
            btnEdit.Image = global::BaseControls.Properties.Resources.Edit___16x16;
            btnEdit.Name = "btnEdit";
            this.Items.Insert(10, btnEdit);

            this.Items.Add(new ToolStripSeparator());

            btnExcelExport = new ToolStripButton();
            btnExcelExport.Image = global::BaseControls.Properties.Resources.Excel___16x16;
            btnExcelExport.Name = "btnExcelExport";
            this.Items.Insert(13, btnExcelExport);
        }
    }
}

BindingNavigator class has a dedicated Designer, BindingNavigatorDesigner,派生自ToolStripDesigner
Designer 调用 public 虚拟 AddStandardItems() 方法,然后在将 BindingNavigator 添加到 Form 时由 Form Designer 调用。

要使您的按钮正常工作,请在自定义 class 中覆盖此方法并在此处添加新按钮。
现在,您的 ToolStripButtons 已在窗体设计器中序列化,如果您双击自定义按钮之一,则会向窗体添加一个 Click 处理程序。

您可以在 .Net Source Code:

中找到注释

Override this method in derived classes to define additional or alternative standard items. To ensure optimal design-time support for your derived class, make sure each item has a meaningful value in its Name property. At design time, this will be used to generate a unique name for the corresponding member variable. The item's Name property will then be updated to match the name given to the member variable.


如您所见,现在按钮功能齐全

我建议您像这样修改您的自定义 BindingNavigator:

► 不要命名您的自定义控件 BindingNavigator

public class BindingNavigatorEx : BindingNavigator
{
    private ToolStripItem btnEdit;
    private ToolStripItem btnExcelExport;

    public BindingNavigatorEx() { }

    public override void AddStandardItems()
    {
        base.AddStandardItems();
        this.Items.Add(new ToolStripSeparator());

        btnEdit = new ToolStripButton() {
            Image = Properties.Resources.Edit___16x16,
            Name = "bindingNavigatorButtonEdit",
            DisplayStyle = ToolStripItemDisplayStyle.Image
        };
        this.Items.Add(btnEdit);

        btnExcelExport = new ToolStripButton() {
            Image = Properties.Resources.Excel___16x16,
            Name = "bindingNavigatorButtonExcelExport",
            DisplayStyle = ToolStripItemDisplayStyle.Image
        };
        this.Items.Add(btnExcelExport);
    }
}