首页 > 解决方案 > 自定义 DataGridViewButtonCell 上的右对齐按钮

问题描述

我想创建一个DataGridViewCell看起来像这个例子的自定义

在此处输入图像描述

我开始创建这个单元格。起初我从 a 继承DataGridViewButtonCell并覆盖了重要的方法。

    private class DataGridViewAllocationCell : DataGridViewButtonCell
    {
        public void Initialize() // Pseudo Constructor with some arguments
        {
            contextMenu = new ContextMenuStrip();
            // fill the contextMenu here
        }

        private ContextMenuStrip contextMenu;

        private const string BUTTON_TEXT = "...";

        private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
        private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
            Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
            Rectangle cellRectangle = GetContentBounds(rowIndex);
            Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
            cellRectangle.Offset(displayRectangle.Location);
            base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
            TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
        }

        protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
        {
            Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
            return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);
        }

        protected override void OnContentClick(DataGridViewCellEventArgs e)
        {
            base.OnContentClick(e);
            Rectangle contentRectangle = GetContentBounds(e.RowIndex);
            Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
            Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
            contextMenu.Show(DataGridView, location);
        }
    }

使用这些单元格创建列时,我得到了这个网格

在此处输入图像描述

重要的部分是第二列。按钮控件正在填充单元格的其余部分。

有没有办法使按钮与其文本一样大(默认宽度)并在右侧对齐?

标签: c#winforms

解决方案


我建议您采用在DataGridViewTextBoxCell焦点集中时覆盖和插入按钮的方法,而不是覆盖DataGridViewButtonCell和绘制文本部分。

唉,你在那里所做的仍然有效,如果你希望按钮以不同的方式对齐,你只需要指定不同的内容边界,即而不是:

return new Rectangle(rectangle.Left + LabelWidth, rectangle.Top, rectangle.Width - LabelWidth, rectangle.Height);

你可以放这样的东西,一个 22 像素宽的右对齐按钮:

return new Rectangle(rectangle.Width - 22, rectangle.Top, 22, rectangle.Height);

您的按钮将表现得像您期望的典型“...”按钮。无论如何,这就是您问题的关键。


推荐阅读