扩展 BindingNavigator

Extending BindingNavigator

我想扩展 BindingNavigator so I can add extra functionality to it. One of the things I want to do is add a ToolStripSplitButton 来自动调整 DataGridView 中的单元格。我已经能够添加按钮,但是当我将控件放在窗体上时,我的按钮位于第一个位置。我想在删除按钮之后添加此按钮。我该怎么做?

设计时将控件拖放到窗体上时如下所示:

代码如下:

public class DataGridToolStrip : BindingNavigator
{

    private ToolStripSplitButton AutoSizeButton;
    private ToolStripMenuItem mnuAllCells;
    private ToolStripMenuItem mnuAllCellsExceptHeader;
    private ToolStripMenuItem mnuColumnHeader;
    private ToolStripMenuItem mnuDisplayedCells;
    private ToolStripMenuItem mnuDisplayedCellsExceptHeader;

    public DataGridToolStrip() : base(false)
    {
        //this.Items.Clear();
        //this.AddStandardItems();

        this.mnuAllCells = new ToolStripMenuItem();
        this.mnuAllCellsExceptHeader = new ToolStripMenuItem();
        this.mnuColumnHeader = new ToolStripMenuItem();
        this.mnuDisplayedCells = new ToolStripMenuItem();
        this.mnuDisplayedCellsExceptHeader = new ToolStripMenuItem();
        this.AutoSizeButton = new ToolStripSplitButton();

        this.AutoSizeButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
        this.AutoSizeButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.mnuAllCells,
        this.mnuAllCellsExceptHeader,
        this.mnuColumnHeader,
        this.mnuDisplayedCells,
        this.mnuDisplayedCellsExceptHeader});

        this.AutoSizeButton.Name = "AutoSizeButton";
        this.AutoSizeButton.Size = new System.Drawing.Size(72, 22);
        this.AutoSizeButton.Text = "Auto Size";



        this.Items.Add(AutoSizeButton);
    }
}

您可以覆盖 BindingNavigatorAddStandardItems 方法,并在调用 base.AddStandardItems() 后添加其他项目:

public class DataGridToolStrip : BindingNavigator
{
    public override void AddStandardItems()
    {
        base.AddStandardItems();
        // Add addtional items here
    }
}