我在写一个自定义的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

这些性质与过载它们的默认值就好了。其时,我开始试图让默认单元格样式,我遇到了这个问题。由于的DataGridViewCellStyle是一类的,我不能让一个不断出来。我已经尝试了所有的设置改变什么,我想他们是在类的构造函数,并且除了在设计特性所做的更改一下就尽快重新设置为应用程序运行,工程巨大。所以把在构造函数中不会做的修改。

还有其它地方我可以把只有当控制将被首先丢弃在设计上运行的代码?或设置默认的任何其他方式?

有帮助吗?

解决方案 2

其实,我想了一段时间,并跨越一个简单的解决方案来为我的问题。因为它依赖于一个事实,即使用自定义组件的人可能永远都不会要恢复整个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

只是表明,仅仅因为你有一个金色的锤子,并不意味着每一个问题都是钉子。

其他提示

我遇到这个问题太大。我的解决方案围绕工作为默认值参数的要求是一个编译时间常数。我想,不会是足以使在类的构造的值(由C#中的静态构造限定,并且在VB共用构造),而不是

这似乎是在我的情况下,良好的变通,但有可能是它有可能打破,因为它直到类的构造函数是在加载类叫做实际上并不是存在于元数据的情况下,但对于一个设计师属性应该是可接受的。因为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); }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top