c# - 自定义 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);
}
}
使用这些单元格创建列时,我得到了这个网格
重要的部分是第二列。按钮控件正在填充单元格的其余部分。
有没有办法使按钮与其文本一样大(默认宽度)并在右侧对齐?
解决方案
我建议您采用在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);
您的按钮将表现得像您期望的典型“...”按钮。无论如何,这就是您问题的关键。
推荐阅读
- javascript - JavaScript:如何异步自动递增计数器
- sql - SQLite 显示指数数字而不是 0
- c - 使用来自用户的 2 个输入来计算 C 语言中的产品价格的程序
- c# - 如何在 DataGridCheckBoxColumn 中添加 Checked 和 Unchecked 事件?
- bash - AWK 划分第一行和第二行
- php - Symfony 包配置 - 条件定义
- javascript - 材料 ui 自动完成无法将选项参数识别为数组
- angular - 对象的 Angular ngFor
- python - 将鼠标悬停在 QMenuBar 中的按钮上后,在 StatusBar 中写入的消息会更改,但当鼠标离开按钮时不会恢复
- azure-data-explorer - 停止/取消引入 Azure 数据资源管理器 (LightIngest)