【问题标题】:How to change the ReadOnly attribute in realtime for use with PropertyGrid如何实时更改 ReadOnly 属性以与 PropertyGrid 一起使用
【发布时间】:2023-04-02 22:20:01
【问题描述】:

我有一个属性类,我发送到 PropertyGrid。我想实时更改某些属性的[readonly] 属性。以下是 vb.net 中此类属性的示例...

<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

【问题讨论】:

    标签: .net vb.net attributes real-time propertygrid


    【解决方案1】:

    据我所知,元数据是静态的,因此无法在运行时更改。 我建议你创建一个包装类。

    【讨论】:

      【解决方案2】:

      是的,您可以在课堂上使用类似于以下的方法来做到这一点:

      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
      

      注意:您需要将类中每个属性的&lt;[ReadOnly]()&gt;&lt;ReadOnlyAttribute()&gt; 设置为某个默认值才能正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 2010-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多