【问题标题】:Deriving DataGridViewButtonColumn class and changing Button object派生 DataGridViewButtonColumn 类并更改 Button 对象
【发布时间】: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)都有自己的整数值?它是否适用于所有属性?我怀疑它有记录吗?

编辑:我是否正确地看到按钮是在单元格内绘制的,而不是真正作为对象创建的?

【问题讨论】:

    标签: c# winforms button


    【解决方案1】:

    看看这个解决方案:

    Change Color of Button in DataGridView Cell

    但是阅读所选答案,我认为您应该关注 Mike Yasieniuk 提供的答案,他的答案实际上得到了更多的支持。

    希望这会有所帮助。

    【讨论】:

    • 不幸的是不是因为它是单元格的前景色。我的重要提示是它不应该仅限于前景色。我还想更改按钮本身的边框和填充。此类属性在DefaultCellStyle 中不可用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 2021-10-07
    • 2023-03-14
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多