2011-04-11 34 views
0

我收到了一個屬性類,我發送給PropertyGrid。我想實時更改某些屬性的[readonly]屬性。下面是在vb.net這種屬性的例子...如何實時更改ReadOnly屬性以便與PropertyGrid一起使用

<CategoryAttribute("Graph Limits"), _ 
     Browsable(True), _ 
     [ReadOnly](False), _ 
     BindableAttribute(False), _ 
     DefaultValueAttribute(100), _ 
     DesignOnly(False), _ 
     DescriptionAttribute("Maximum value")> _ 
Public Property Max() As Double 
    Get 
     Return _Max 
    End Get 
    Set(ByVal Value As Double) 
     _Max = Value 
    End Set 
End Property 

回答

0

元數據是靜態的,據我所知,所以它不能在運行時改變。 我建議你創建一個包裝類。

Private Sub SetReadOnlyProperty(ByVal propertyName As String, ByVal [readOnly] As Boolean) 
    Dim descriptor As PropertyDescriptor = TypeDescriptor.GetProperties([GetType]())(propertyName) 
    Dim attribute As ReadOnlyAttribute = DirectCast(descriptor.Attributes(GetType(ReadOnlyAttribute)), ReadOnlyAttribute) 
    Dim fieldToChange As FieldInfo = attribute.[GetType]().GetField("isReadOnly", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance) 
    fieldToChange.SetValue(attribute, [readOnly]) 
End Sub 

注:

0

是的,你可以通過使用類似於您類以下的方法做到這一點,你需要設置的每種屬性的<[ReadOnly]()><ReadOnlyAttribute()>在類的一些默認值爲此工作。

相關問題