문제

대규모 프로젝트를 위해 사용자 정의 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

사실, 나는 그것에 대해 더 오래 생각했고 내 문제에 대한 더 간단한 솔루션을 발견했습니다. 사용자 지정 구성 요소를 사용하는 사람이 전체 셀 스타일을 Windows 기본값으로 되돌리고 싶지 않을 것이라는 사실에 의존하기 때문에 모든 경우에 효과가 없습니다. 나는 새로운 셀 스타일을 생성자의 현재 셀 스타일과 비교하고 일치하는 경우 스타일 만 설정했습니다. 이렇게하면 변경 사항을 덮어 쓰지 않지만 처음으로 변경 사항을 설정합니다.

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

당신이 황금 망치를 가지고 있기 때문에 보여 주러 가면 모든 문제가 손톱이라는 것을 의미하지는 않습니다.

다른 팁

나도이 문제를 해결했다. 내 솔루션은 DefaultValue 인수가 컴파일 타임 상수가되기위한 요구 사항을 중심으로 작동합니다. 클래스 생성자 (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