【发布时间】:2015-07-09 17:51:12
【问题描述】:
我试图创建一个DataGridViewButtonColumn 的子类,但我被卡住了。我现在可以轻松更改的唯一属性是FlatStyle,它是 Button 类的一部分。假设我想更改ForeColor(还有其他事情,ForeColor 只是一个示例,所以请不要坚持,即不要使用像DefaultCellStyle 这样的解决方法)。因此,我必须以某种方式从DataGridViewButtonColumn 检索 Button 对象。在DataGridViewButtonColumn 里面有DataGridViewButtonCell。这个类(我相信)拥有 Button 并且它具有名为 Value 的属性(派生自 DataGridViewCell),但它在 DataGridViewButtonCell 的构造函数中为空,因此我无法从那里访问它。否则我如何对单元格内的 Button 对象产生任何影响?
这是我正在尝试做的示例:
public class MyDataGridViewButtonColumn : DataGridViewButtonColumn
{
public MyDataGridViewButtonColumn () : base()
{
((Button)((DataGridViewButtonCell)this.CellTemplate).Value).ForeColor = Color.Red;
}
}
我不熟悉使用现有的 .NET 控件进行操作,但 MSDN 不“支持”它,这意味着他们没有真正记录如何操作的示例。我的尝试基于研究 .NET 框架的代码并从中学习。
我检查了他们如何实现 FlatStyle,我认为它会调用 Button 对象并将值设置为我指定的任何值。但是,我不知道该实现。
public FlatStyle FlatStyle
{
set
{
if (!ClientUtils.IsEnumValid(value, (int)value, 0, 3))
{
throw new InvalidEnumArgumentException("value", (int)value, typeof(FlatStyle));
}
if (value != this.FlatStyle)
{
base.Properties.SetInteger(DataGridViewButtonCell.PropButtonCellFlatStyle, (int)value);
base.OnCommonChange();
}
}
}
看起来它改变了一些Properties 类,其中每个属性(如FlatStyle)都有自己的整数值?它是否适用于所有属性?我怀疑它有记录吗?
编辑:我是否正确地看到按钮是在单元格内绘制的,而不是真正作为对象创建的?
【问题讨论】: