2011-06-02 1 views
2

私は最近、プロジェクトのmy.settingsオブジェクトでpropertygridオブジェクトの使用法を学びました。私はこれが非常に好きです。下位の説明パネルのプロパティグリッドに含まれるように、設定に説明を含める方法があるかどうか疑問に思っていましたか?my.settingsとその説明?そして、それらをプロパティグリッドに入れるのですか?

私はこれをカバーする古い(2003年)codeproj記事を見つけましたが、多くのカスタム作業が必要であり、より簡単な方法が出てくることを期待していました。

答えて

0

設定値を取得/設定するだけの簡単なクラスの設定でアプリケーションの各設定のラップを実行し、そのラッパークラスの各プロパティに[Description("Some descr")]属性を追加すると、PropertyGridの説明を表示できます。

0

DToolsには、プロキシ設定オブジェクトの維持を容易にするクラスのペアが含まれています。 PropertyGridで動作する説明とデフォルトの属性を定義し、settingsプロパティで対応する属性を参照して値を取得します。これにより、プロキシ設定オブジェクトでこれらの属性を手動で更新することを忘れずに、設定デザイナを使用して説明とデフォルト値を維持することができます。

''' <summary><see cref="DescriptionAttribute"/> that takes its value from <see cref="System.Configuration.SettingsDescriptionAttribute"/></summary> 
''' <author www="http://dzonny.cz">Đonny</author> 
''' <version version="1.5.2" stage="RC"><see cref="VersionAttribute"/> and <see cref="AuthorAttribute"/> removed</version> 
Public Class SettingsInheritDescriptionAttribute : Inherits DescriptionAttribute 
    ''' <summary>CTor</summary> 
    ''' <param name="Settings">The data type that contains property with name specified in <paramref name="Property"/></param> 
    ''' <param name="Property">Name of the property which's <see cref="SettingsDescriptionAttribute"/> initializes this attribute</param> 
    ''' <param name="AlternateDescription">Alternative description used in case of failure of getting description form specified property</param> 
    Public Sub New(ByVal Settings As Type, ByVal [Property] As String, Optional ByVal AlternateDescription As String = "") 
     '#If VBC_VER >= 9 Then 
     MyBase.New(If(AlternateDescription = "", [Property], AlternateDescription)) 
     '#Else 
     '   MyBase.New(iif(AlternateDescription = "", [Property], AlternateDescription)) 
     '#End If 
     Me.Settings = Settings 
     Me.Property = [Property] 
    End Sub 
    ''' <summary>The data type that contains property with name spacified in <see cref="[Property]"/></summary> 
    Private Settings As Type 
    ''' <summary>Name of the property which's <see cref="SettingsDescriptionAttribute"/> initializes this attribute</summary> 
    Private [Property] As String 
    ''' <summary>Gets or sets the string stored as the description.</summary> 
    ''' <returns>The string stored as the description. The default value is an empty string ("").</returns> 
    Public Overrides ReadOnly Property Description() As String 
     Get 
      Dim sds As Object() = Settings.GetProperty([Property]).GetCustomAttributes(GetType(SettingsDescriptionAttribute), True) 
      If sds IsNot Nothing AndAlso sds.Length > 0 Then 
       Return CType(sds(0), SettingsDescriptionAttribute).Description 
      Else 
       Return MyBase.DescriptionValue 
      End If 
     End Get 
    End Property 
End Class 
''' <summary><see cref="DefaultValueAttribute"/> that takes its value from <see cref="System.Configuration.DefaultSettingValueAttribute"/></summary> 
''' <author www="http://dzonny.cz">Đonny</author> 
''' <version version="1.5.2" stage="RC"><see cref="VersionAttribute"/> and <see cref="AuthorAttribute"/> removed</version> 
Public Class SettingsInheritDefaultValueAttribute : Inherits DefaultValueAttribute 
    ''' <summary>CTor</summary> 
    ''' <param name="Settings">The data type that contains property with name defined in <paramref name="Property"/></param> 
    ''' <param name="Property">Name of property from which's <see cref="DefaultSettingValueAttribute"/> this attribute is initialized</param> 
    ''' <param name="Type">The data type of the value</param> 
    ''' <param name="AlternateDefaultValue">Alternative default value used when fetching fails</param> 
    Public Sub New(ByVal Settings As Type, ByVal [Property] As String, ByVal Type As Type, Optional ByVal AlternateDefaultValue As String = "") 
     MyBase.New(Type, AlternateDefaultValue) 
     Me.Settings = Settings 
     Me.Property = [Property] 
     Me.ValueType = Type 
    End Sub 
    ''' <summary>CTor for default values of <see cref="String"/> type</summary> 
    ''' <param name="Settings">The data type that contains property with name defined in <paramref name="Property"/></param> 
    ''' <param name="Property">Name of property from which's <see cref="DefaultSettingValueAttribute"/> this attribute is initialized</param> 
    Public Sub New(ByVal Settings As Type, ByVal [Property] As String) 
     Me.New(Settings, [Property], GetType(String)) 
    End Sub 
    ''' <summary>Contains value of the <see cref="Settings"/> property</summary> 
    <EditorBrowsable(EditorBrowsableState.Never)> Private _Settings As Type 
    ''' <summary>Contains value of the <see cref="[Property]"/> property</summary> 
    <EditorBrowsable(EditorBrowsableState.Never)> Private [_Property] As String 
    ''' <summary>Contains value of the <see cref="ValueType"/> property</summary> 
    <EditorBrowsable(EditorBrowsableState.Never)> Private _ValueType As Type 
    ''' <summary>Gets the default value of the property this attribute is bound to.</summary> 
    ''' <returns>An <see cref="System.Object"/> that represents the default value of the property this attribute is bound to.</returns> 
    ''' <remarks>Default values can be obtained if stored in form that can be directly returned or if stored as XML-serialized values.</remarks> 
    Public Overrides ReadOnly Property Value() As Object 
     Get 
      Dim sds As Object() = Settings.GetProperty([Property]).GetCustomAttributes(GetType(DefaultSettingValueAttribute), True) 
      If sds IsNot Nothing AndAlso sds.Length > 0 Then 
       Try 
        Dim mySerializer As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(ValueType) 
        Dim stream As New System.IO.StringReader(CType(sds(0), DefaultSettingValueAttribute).Value) 
        Return mySerializer.Deserialize(stream) 
       Catch 
        Dim a As New DefaultValueAttribute(ValueType, CType(sds(0), DefaultSettingValueAttribute).Value) 
        Return a.Value 
       End Try 
      Else 
       Return MyBase.Value 
      End If 
     End Get 
    End Property 
    ''' <summary>The data type that contains property with name defined in <see cref="[Property]"/></summary> 
    Public Property Settings() As Type 
     Get 
      Return _Settings 
     End Get 
     Protected Set(ByVal value As Type) 
      _Settings = value 
     End Set 
    End Property 
    ''' <summary>Name of property from which's <see cref="DefaultSettingValueAttribute"/> this attribute is initialized</summary> 
    Public Property [Property]() As String 
     Get 
      Return [_Property] 
     End Get 
     Protected Set(ByVal value As String) 
      [_Property] = value 
     End Set 
    End Property 
    ''' <summary>The data type of the value</summary> 
    Public Property ValueType() As Type 
     Get 
      Return _ValueType 
     End Get 
     Protected Set(ByVal value As Type) 
      _ValueType = value 
     End Set 
    End Property 
End Class 

このようにそれを使用します。

Public Class ProxySettings 
    ''' <summary>Wraps <see cref="My.MySettings.SomeSetting"/> property</summary> 
    <SettingsInheritDescription(GetType(My.MySettings), "SomeSetting")> _ 
    <SettingsInheritDefaultValue(GetType(My.MySettings), "SomeSetting")> _ 
    Public Property SomeSetting() As Decimal 
     Get 
      Return My.Settings.SomeSetting 
     End Get 
     Set(ByVal value As Decimal) 
      My.Settings.SomeSetting = value 
     End Set 
    End Property 
End Class 
0

これは行うことは非常に簡単で、は、任意のカスタムクラスを必要としません。ソリューションエクスプローラでSettings.Designer.csSettings.settingsPropertiesと開いてください。すべてのプロパティのためにあなたが追加することにより、PropertyGridによって読み取り可能な説明を追加することができます。

[Description("Your custom description here")] 

同じことを行うことができます。

[Category("Your custom category here")] 

これらはusing System.ComponentModel;が必要です。

あなたの説明は、あなたのPropertyGridで期待どおりに表示され、他の作業は必要ありません。

注:ファイルが再生成されると、変更が失われる可能性があるため、自動生成ツールで編集する予定がある場合は注意が必要です。

関連する問題