我的 .net 核心 Winform 資料 GridVew 中有一個復選框列。
我已經使復選框更大,以便用戶單擊。
我的代碼里面CellPainting,
e.PaintBackground(e.CellBounds, true);
ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X 1, e.CellBounds.Y 5,
e.CellBounds.Width - 10, e.CellBounds.Height - 10,
(bool)e.FormattedValue ? ButtonState.Checked : ButtonState.Normal);
e.Handled = true;
這些是我的參考鏈接,

但問題是點擊區域,即使復選框的大小更大,我意識到可點擊區域仍然是原來的大小。
如下所示,綠色區域是可點擊區域,

我想讓可點擊區域與復選框的大小一樣大,如下所示,

有什么解決辦法嗎?
uj5u.com熱心網友回復:
當您單擊滑鼠DataGridViewCheckBoxCell來切換Checked狀態時,您必須單擊單元格內的內容,單擊單元格內的其他位置不會改變任何事情。a 的內容DataGridViewCheckBoxCell就是那個小盒子區域。因此,僅繪制一個更大的框不會調整該內容DefaultCellStyle.Alignment區域的大小或重新定位(根據該列的),并且它保持不變。您需要為此撰寫代碼來說明,內容區域已被單擊,并且應該呼叫相關的基本方法和事件。
我會創建自定義DataGridViewCheckBox列和單元格來應用此要求。
在專案的命名空間中,從以下位置派生一個新類DataGridViewCheckBoxColumn:
public class CustomDataGridViewCheckBoxColumn : DataGridViewCheckBoxColumn
{
public CustomDataGridViewCheckBoxColumn() : base() =>
CellTemplate = new CustomDataGridViewCheckBoxCell();
public override DataGridViewCell CellTemplate
{
get => base.CellTemplate;
set
{
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CustomDataGridViewCheckBoxCell)))
throw new InvalidCastException("CustomDataGridViewCheckBoxCell.");
base.CellTemplate = value;
}
}
[Category("Appearance")]
[DefaultValue(typeof(Size), "17, 17")]
[Description("The size of the check box.")]
public Size CheckBoxSize { get; set; } = new Size(17, 17);
// We should copy the new properties.
public override object Clone()
{
var c = base.Clone() as CustomDataGridViewCheckBoxColumn;
c.CheckBoxSize = CheckBoxSize;
return c;
}
}
另一個來自DataGridViewCheckBoxCell:
public class CustomDataGridViewCheckBoxCell : DataGridViewCheckBoxCell
{
private Rectangle curCellBounds;
private Rectangle checkBoxRect;
public CustomDataGridViewCheckBoxCell() : base() { }
protected override void Paint(
Graphics g,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates elementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// Paint default except the check box parts.
var parts = paintParts & ~(DataGridViewPaintParts.ContentForeground
| DataGridViewPaintParts.ContentBackground);
base.Paint(g,
clipBounds,
cellBounds,
rowIndex,
elementState,
value,
formattedValue,
errorText,
cellStyle,
advancedBorderStyle,
parts);
if (curCellBounds != cellBounds)
{
// To get the box size...
var col = OwningColumn as CustomDataGridViewCheckBoxColumn;
curCellBounds = cellBounds;
// ToDo: Use col.DefaultCellStyle.Alignment or
// DataGridView.ColumnHeadersDefaultCellStyle.Alignment
// to position the box. MiddleCenter here...
checkBoxRect = new Rectangle(
(cellBounds.Width - col.CheckBoxSize.Width) / 2 cellBounds.X,
(cellBounds.Height - col.CheckBoxSize.Height) / 2 cellBounds.Y,
col.CheckBoxSize.Width,
col.CheckBoxSize.Height);
}
ControlPaint.DrawCheckBox(g, checkBoxRect, (bool)formattedValue
? ButtonState.Checked | ButtonState.Flat
: ButtonState.Flat);
}
// In case you don't use the `Alignment` property to position the
// box. This is to disallow toggling the state if you click on the
// original content area outside the drawn box.
protected override void OnContentClick(DataGridViewCellEventArgs e)
{
if (!ReadOnly &&
checkBoxRect.Contains(DataGridView.PointToClient(Cursor.Position)))
base.OnContentClick(e);
}
protected override void OnContentDoubleClick(DataGridViewCellEventArgs e)
{
if (!ReadOnly &&
checkBoxRect.Contains(DataGridView.PointToClient(Cursor.Position)))
base.OnContentDoubleClick(e);
}
// Toggle the checked state by mouse clicks...
protected override void OnMouseUp(DataGridViewCellMouseEventArgs e)
{
base.OnMouseUp(e);
if (!ReadOnly && e.Button == MouseButtons.Left &&
checkBoxRect.Contains(DataGridView.PointToClient(Cursor.Position)))
{
Value = Value == null || !Convert.ToBoolean(Value);
DataGridView.RefreshEdit();
DataGridView.NotifyCurrentCellDirty(true);
}
}
// ... and Space key...
protected override void OnKeyDown(KeyEventArgs e, int rowIndex)
{
base.OnKeyDown(e, rowIndex);
if (!ReadOnly && e.KeyCode == Keys.Space)
{
Value = Value == null || !Convert.ToBoolean(Value.ToString());
DataGridView.RefreshEdit();
DataGridView.NotifyCurrentCellDirty(true);
}
}
}
在網格設計器中重建并檢查新的列型別。

如果您有資料系結網格,請將AutoGenerateColumns屬性設定為false并手動添加列。例如:
private readonly static Random rnd = new Random();
//..
var dt = new DataTable();
dt.Columns.AddRange(new[]
{
new DataColumn("Default", typeof(bool)),
new DataColumn("Custom", typeof(bool))
});
for (int i = 1; i < 6; i )
dt.Rows.Add(rnd.Next(0, 2), rnd.Next(0, 2));
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns.AddRange(new[]
{
new DataGridViewCheckBoxColumn { HeaderText = "Default" },
new CustomDataGridViewCheckBoxColumn
{
HeaderText = "Custom",
CheckBoxSize = new Size(32, 32)
}
});
dataGridView1.DataSource = dt;

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/506814.html
標籤:C# 表格 复选框 数据网格视图 数据网格视图复选框单元格
