派生DataGridViewButtonColumn类并更改Button对象

时间:2015-07-09 17:51:12

标签: c# winforms button

我试图创建一个DataGridViewButtonColumn的子类,但我被卡住了。我现在可以轻松更改的唯一属性是FlatStyle,它是Button类的一部分。说我想更改ForeColor(还有其他一些事情,ForeColor只是一个例子,所以请不要坚持这一点,即不要使用{{{}之类的解决方法1}})。因此,我必须以某种方式从DefaultCellStyle检索Button对象。在DataGridViewButtonColumnDataGridViewButtonColumn。这个类(我相信)拥有Button并且它具有名为Value的属性(派生自DataGridViewButtonCell),但它在DataGridViewCell的构造函数中为空,因此我无法访问它从那里。否则我怎么能对单元格内的Button对象产生影响?

以下是我尝试做的例子:

DataGridViewButtonCell

我很擅长使用现有的.NET控件进行操作,但MSDN并不支持"支持"它,这意味着他们并没有真正记录如何这样做的例子。我的尝试是基于查看.NET框架的代码并从那里学习。

我检查了他们如何实现FlatStyle,我认为它会调用Button对象并将值设置为我指定的任何值。但是,实施对我来说是未知的。

public class MyDataGridViewButtonColumn : DataGridViewButtonColumn
{
    public MyDataGridViewButtonColumn () : base()
    {
        ((Button)((DataGridViewButtonCell)this.CellTemplate).Value).ForeColor = Color.Red;
    }
}

看起来它改变了一些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)都有它自己的整数值?它对所有属性的工作方式是否相同?我怀疑它有记录吗?

编辑:我是否正确地看到Button实际上是在单元格内绘制而不是真正创建为对象?

1 个答案:

答案 0 :(得分:0)

查看此解决方案:

Change Color of Button in DataGridView Cell

但是读过选定的答案,我认为你应该专注于Mike Yasieniuk提供的答案,他的回答实际上更多。

希望这会有所帮助。