2

커다란 프로젝트를 위해 커스텀 DataGridView 객체를 작성하여 많은 개발자들에게 앱 섹션을 일관성있게 보이게 만듭니다.DataGridViewCellStyle 오버로드하고 기본값을 부여하십시오.

는 내가있는 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은 클래스이므로 상수를 만들 수 없습니다. 모든 설정을 클래스 생성자에서 원하는대로 변경하려고 시도했습니다. 디자이너 속성의 변경 사항이 앱 실행과 동시에 다시 설정된다는 점만 제외하면 멋지게 작동합니다. 그래서 생성자에 변경 사항을 추가하는 것은 불가능합니다.

컨트롤이 디자이너에서 처음 삭제 될 때만 실행되는 코드를 어디에도 둘 수 있습니까? 또는 기본값을 설정하는 다른 방법은 무엇입니까?

답변

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 

황금 망치가있는 것만으로 모든 문제가 손톱임을 의미하지는 않습니다.

+0

오른쪽 일 수도 있습니다. – broke

+0

그런 식으로. – Jrud

2

이 문제도 발생했습니다. 내 솔루션은 DefaultValue 인수가 컴파일 타임 상수가되도록 요구 사항을 해결합니다. 클래스 생성자 (C#에서는 정적 생성자로, VB에서는 공유 생성자로 정의 됨)의 값을 설정하는 것으로 충분하지 않습니까?

이것은 내 작업에서 좋은 방법 인 것처럼 보이지만 클래스를로드 할 때 클래스 생성자가 호출 될 때까지 실제로 메타 데이터에 존재하지 않기 때문에 깨질 수도 있습니다. 허용되어야하는 디자이너 특성. DefaultValueAttribute.SetValue는 보호되어 있으므로 public으로 만드는 파생 클래스를 정의해야했습니다.

이 값은 디자이너에서 올바르게 작동하며 값이 기본값과 같을 때 인식하고 가능한 경우 생성 된 코드에서 생략하고 기본값과의 차이점 만 생성합니다.

여기에 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); } 
} 
관련 문제