簡體   English   中英

.net中的類級別的RefreshProperties屬性,winforms +錯誤地刷新了屬性網格

[英]RefreshProperties attribute at class level in .net, winforms + incorrectly refreshing property grid

我在屬性網格中編輯類時遇到了一個奇怪的問題,即屬性網格將無法正確刷新。

我設法將問題簡化為只有兩個屬性的類。 為了方便說明,我在最后添加了代碼。

它基本上可以歸結為具有兩個屬性的類。 第一個是可擴展的(字體)。 該類本身是可擴展的,並且還在類型轉換器中實現CreateInstance方法。

要查看該問題,請展開字體,進行編輯,說“粗體”,然后移開標簽。 發生兩個問題:

(1)第二個屬性跳轉並在擴展的font屬性中結束。

(2)擴展字體的“-”符號變為“ +”。

通過將ResfreshProperties(RefreshProperties.All)附加到類中,問題就解決了。

很好,但是我想了解它是如何解決問題的。 我看過反射器,找不到在類級別附加的RefreshProperties的任何示例。

///簡單的類

<TypeConverter(GetType(Class1Converter)), _
RefreshProperties(RefreshProperties.All)> _
Public Class Class1

Public Sub New(ByVal font As Font, ByVal image As Image)
    Me.New()
    Me.Image = image
    Me.Font = font
End Sub

Public Sub New()
End Sub

Private _Font As Font = New Font("Arial", 10)
Public Property Font() As Font
    Get
        Return _Font
    End Get
    Set(ByVal value As Font)
        _Font = value
    End Set
End Property

Private _Image As Image
Public Property Image() As Image
    Get
        Return _Image
    End Get
    Set(ByVal value As Image)
        _Image = value
    End Set
End Property

End Class

///該類的轉換器

Public Class Class1Converter
Inherits ExpandableObjectConverter

Public Overrides Function GetCreateInstanceSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
    Return True
End Function

Public Overrides Function CreateInstance(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal propertyValues As System.Collections.IDictionary) As Object
    Dim font As Font = TryCast(propertyValues("Font"), Font)
    Dim image As Image = CType(propertyValues("Image"), Image)
    Return New Class1(font, image)
End Function

End Class

///主持課程的按鈕

Public Class MyButton
Inherits Button

Private _C As Class1 = New Class1
Public Property C() As Class1
    Get
        Return _C
    End Get
    Set(ByVal value As Class1)
        _C = value
    End Set
End Property

末級

聽起來正在發生的是屬性網格控件中的繪畫錯誤。 發生錯誤時,是否在屬性級別應用了RefreshPropertiesAttribute? 組件是否實現INotifyPropertyChanged?

該問題可能消失的原因之一是,在類級別應用RefreshPropertiesAttribute並不是應該使用該屬性的方式。 我猜想,通過在班級應用它,您可以有效地刪除它。 看起來他們將其設置為AttributeTargets.All但顯然這不是設計使然。

RefreshProperties是一種保護屬性。 它告訴設計者,當此屬性的值更改時(通過屬性網格),請重新查詢該類上的所有其他屬性。 顯然,這是一種浪費,特別是如果已更改的屬性對任何其他屬性都沒有任何影響。

Changed event pattern. 如果確實具有導致其他屬性發生更改的屬性,則可以使用 Changed事件模式。 在這種情況下,當這些屬性更改時,您將在類上引發FontChanged或ImageChanged事件。 Windows窗體設計器使用該命名約定查找事件,並使用它們使屬性網格無效。

但是在您給出的示例中,這兩個屬性不會彼此無效,因此您無需使一個屬性無效即可響應另一個屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM