扩展BindingNavigator

时间:2016-12-10 17:50:56

标签: c# winforms bindingnavigator

我想扩展BindingNavigator,以便我可以为其添加额外的功能。我想要做的一件事是添加一个ToolStripSplitButton,它将自动调整DataGridView中的单元格。我已经能够添加按钮,但是当我将控件放在窗体上时,我的按钮位于第一个位置。我想在删除按钮后添加此按钮。我怎么能这样做?

以下是在设计时放入表单时控件的外观: nav

以下是代码:

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

1 个答案:

答案 0 :(得分:1)

您可以覆盖BindingNavigator的{​​{3}}方法,并在致电base.AddStandardItems()后添加其他项目:

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