重载DataGridViewCellStyle并提供默认值

时间:2010-02-10 19:26:28

标签: vb.net datagridview properties default-value datagridviewcellstyle

我正在为一个大型项目编写一个自定义DataGridView对象,以便向一群开发人员分发,以使我们的应用程序部分看起来一致。

我想为DataGridView的许多属性设置默认值,我可以设置其中许多属性:

<System.ComponentModel.Browsable(True), System.ComponentModel.DefaultValue(DataGridViewAutoSizeColumnsMode.Fill)>_
Public Overloads Property AutoSizeColumnsMode() As DataGridViewAutoSizeColumnMode
    Get
        Return MyBase.AutoSizeColumnsMode
    End Get
    Set(ByVal value As DataGridViewAutoSizeColumnMode)
        MyBase.AutoSizeColumnsMode = value
    End Set
End Property

这些属性超载了它们的默认值就好了。当我开始尝试制作我遇到问题的默认Cell样式时。由于DataGridViewCellStyle是一个类,我无法使它成为常量。我已经尝试将所有设置更改为我希望它们在类构造函数中的设置,这很有效,除了设计器属性中所做的更改只是在应用程序运行后立即恢复。因此,将更改放在构造函数中是行不通的。

有没有其他地方我可以把代码只在控件首次放到设计器上时运行?或任何其他设置默认的方式?

2 个答案:

答案 0 :(得分:2)

我也遇到了这个问题。我的解决方案是将DefaultValue参数的要求作为编译时常量。我想,在类构造函数中设置值(由C#中的静态构造函数定义,以及VB中的共享构造函数)是不是足够了?

在我的情况下,这似乎是一个很好的解决方法,尽管有可能会破坏它的实例,因为在加载类之前调用​​类构造函数之前它实际上并不存在于元数据中,但是对于Designer来说属性应该是可以接受的。因为DefaultValueAttribute.SetValue受到保护,所以我必须定义一个派生类,使其公开。

这在设计器中工作正常,它可以识别值与默认值相同,并在可能的情况下从生成的代码中省略它,并且只生成与默认值的差异。

这是C#中的代码,这也适用于VB,但我对它的语法不太熟悉,所以我不得不把它留给你。

public partial class HighlightGrid : DataGridView
{
    // Class constructor
    static MethodGrid()
    {
        // Get HighlightStyle attribute handle
        DefaultValueSettableAttribute attr =
            TypeDescriptor.GetProperties(typeof(HighlightGrid))["HighlightStyle"]
            .Attributes[typeof(DefaultValueSettableAttribute)]
            as DefaultValueSettableAttribute;

        // Set default highlight style
        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.BackColor = Color.Chartreuse;
        attr.SetValue(style);
    }
    [DefaultValueSettable, Description("Cell style of highlighted cells")]
    public DataGridViewCellStyle HighlightStyle
    {
        get { return this.highlightStyle; }
        set { this.highlightStyle = value; }
    }
    // ...
}

// Normally the value of DefaultValueAttribute can't be changed and has
// to be a compile-time constant. This derived class allows setting the
// value in the class constructor for example.
public class DefaultValueSettableAttribute : DefaultValueAttribute
{
    public DefaultValueSettableAttribute() : base(new object()) { }
    public new void SetValue(Object value) { base.SetValue(value); }
}

答案 1 :(得分:1)

实际上,我考虑了一段时间,并为我的问题找到了一个更简单的解决方案。这并不适用于所有情况,因为它依赖于使用自定义组件的人可能永远不想将整个CellStyle恢复为Windows默认值的事实。我最终将一个新的CellStyle与构造函数中的当前一个进行比较,并且仅在它们匹配时才设置样式。这样它就不会覆盖更改,但会在第一次设置它。

Public Class CustomDataGridView
    Inherits System.Windows.Forms.DataGridView
    Private RowStyle As New DataGridViewCellStyle

    Public Sub New()
        RowStyle.BackColor = Color.FromArgb(223, 220, 200)
        RowStyle.Font = New Font("Arial", 12.75, FontStyle.Bold, GraphicsUnit.Point)
        RowStyle.ForeColor = Color.Black
        RowStyle.SelectionBackColor = Color.FromArgb(94, 136, 161)

        If MyBase.RowsDefaultCellStyle.ToString = (New DataGridViewCellStyle).ToString Then
            MyBase.RowsDefaultCellStyle = RowStyle
        End If 
    End Sub
End Class

只是去展示,只因为你有一把金锤,并不代表每一个问题都是钉子。