C# 如何停止CheckBoxRenderer.DrawCheckBox创建的文本分层
我有以下单元格,用于数据网格上的自定义列数据类型C# 如何停止CheckBoxRenderer.DrawCheckBox创建的文本分层,c#,winforms,.net-3.5,datagridview,C#,Winforms,.net 3.5,Datagridview,我有以下单元格,用于数据网格上的自定义列数据类型 public class DataGridViewReviewerCell : DataGridViewCell { protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeC
public class DataGridViewReviewerCell : DataGridViewCell
{
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return Value;
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates cellState, object value, object formattedValue,
string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, "", "", errorText,
cellStyle, advancedBorderStyle, paintParts);
var parent = (DataGridViewReviewerColumn) OwningColumn;
var columnValue = (ReviewerCheckBox) Value;
CheckBoxRenderer.DrawCheckBox(
graphics,
new Point(cellBounds.X + 4, cellBounds.Y + 4),
new Rectangle(cellBounds.X + 2, cellBounds.Y + 4, cellBounds.Width, cellBounds.Height - (4 * 2)),
" " + columnValue.ReviewerEmployeeName,
parent.InheritedStyle.Font,
TextFormatFlags.Left,
false,
(columnValue.IsChecked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal));
}
}
该类按预期工作,但是每次调用Paint()时,文本(当前为“”+columnValue.ReviewerEmployeeName
)都会不断分层,从而创建非常不可读的文本。我似乎找不到任何能解决这个问题的方法
慢代码
下面是一段运行非常缓慢的代码。当我将其置于调试模式时,它似乎在无休止地运行
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Rows.Add("STEP A", new ReviewerCheckBox());
dataGridView1.Rows.Add("STEP B", new ReviewerCheckBox());
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1 || e.ColumnIndex != 1) return;
var cell = (ReviewerCheckBox) dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
cell.IsChecked = !cell.IsChecked;
cell.ReviewerEmployeeName = (cell.IsChecked ? Environment.UserName : String.Empty);
//MessageBox.Show("Testing");
}
}
public class ReviewerCheckBox
{
public string ReviewerEmployeeName { get; set; }
public bool IsChecked { get; set; }
public bool IsRequired { get; set; }
}
}
public class DataGridViewReviewerColumn : DataGridViewColumn
{
public DataGridViewReviewerColumn()
{
CellTemplate = new DataGridViewReviewerCell();
ReadOnly = false;
SortMode = DataGridViewColumnSortMode.NotSortable;
Resizable = DataGridViewTriState.False;
}
}
您应该检查
paintParts
参数以确定应该绘制哪些零件。例如,如果背景需要绘制,将设置DataGridViewPaintParts.Backgound标志
if (paintParts.HasFlag(DataGridViewPaintParts.Background))
{
using (SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor))
{
graphics.FillRectangle(cellBackground, cellBounds);
}
}
if (paintParts.HasFlag(DataGridViewPaintParts.Border))
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
// Paint you cell content here
下面是一个更完整的示例
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
namespace HideMainWinForm
{
class DataGridViewReviewerCell : DataGridViewCell
{
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
return value;
}
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
if (paintParts.HasFlag(DataGridViewPaintParts.Background))
{
using (SolidBrush cellBackground =
new SolidBrush(cellStyle.BackColor))
{
graphics.FillRectangle(cellBackground, cellBounds);
}
}
if (paintParts.HasFlag(DataGridViewPaintParts.Border))
{
PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
advancedBorderStyle);
}
if (value != null)
{
CheckBoxRenderer.DrawCheckBox(
graphics,
new Point(cellBounds.X + 4, cellBounds.Y + 4),
new Rectangle(cellBounds.X+24,cellBounds.Y+4, cellBounds.Width-24, cellBounds.Height-4),
formattedValue.ToString(),
OwningColumn.InheritedStyle.Font,
TextFormatFlags.Left,
false,
CheckBoxState.CheckedNormal);
}
}
}
}
两个HasFlag
函数调用可以转换为
if ((paintParts & DataGridViewPaintParts.Background) ==
DataGridViewPaintParts.Background)
{
...
}
if ((paintParts & DataGridViewPaintParts.Border) ==
DataGridViewPaintParts.Border)
{
...
}
谢谢你的回复,克里斯。我的
paintParts
参数似乎为空。另外,paintParts
没有HasFlag
方法。@Mike,HasFlag是自framework 3.5以来enum
的成员。paintParts不能为null,因为它是不可为null的值类型(枚举)。因为我使用的是VS2010,所以我切换到了4.0,错误消失了。我将找出如何将其更改为与3.5兼容。谢谢。:)@Mike,MSDN错误地将Enum记录为在Framework 3.5中具有HasFlag方法。我已经添加了备用标志检查,您可以在Framework 4.0之前的框架版本中使用它。关于为什么使用此确切代码会导致Paint()
在无限循环中被调用,您有什么想法吗?