【问题标题】:right aligned button on custom DataGridViewButtonCell自定义 DataGridViewButtonCell 上的右对齐按钮
【发布时间】:2018-11-19 12:39:48
【问题描述】:

我想创建一个类似于此示例的自定义 DataGridViewCell

我开始创建这个单元格。一开始我继承自 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


    【解决方案1】:

    我建议您采用覆盖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);
    

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

    【讨论】:

    • 非常感谢您的回答。您介意为DataGridViewTextBoxCell 解决方案提供完整答案吗?那真是太棒了。目前我只知道DataGridViewButtonCell 解决方案,这对我来说真的很新
    • 我刚刚将22 替换为rectangle.Height,因为它返回值21 我认为这更合适
    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2017-06-13
    • 1970-01-01
    相关资源
    最近更新 更多