如何在Propertygrid中使用dexcription对枚举值进行排序 [英] How to sort Enum values with dexcription in Propertygrid

查看:124
本文介绍了如何在Propertygrid中使用dexcription对枚举值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何通过说明在propertygrid中订购枚举值?



< pre lang =vb> 公共 myObject
公开 枚举 eFolderOrder

< ComponentModel.Description( c)> l = 0
< ComponentModel.Description( 一个)GT; m = 1
< ComponentModel.Description( b)GT; n = 2
结束 枚举


< TypeConverter( GetType (DescriptionConverter))> < System.ComponentModel.DisplayName( MyProp)> 公共 属性 test 作为 eFolderOrder = eFolderOrder。说明
结束





  DescriptionConverter 
继承 EnumConverter
私有 _enumType 作为键入
''' < 摘要 / > 初始化实例< / summary / >
''' < param name =type / > 类型枚举< / param / >
''' 这只是一个函数,你必须
''' 更改。枚举的所有其他功能
''' 您可以通过Ctrl + C / Ctrl +使用V
公开 Sub (类型 As 类型)
MyBase (类型)
_enumType =类型
结束 Sub

公共 覆盖 函数 CanConvertTo(context As ITypeDescriptorContext,destType As Type) As Boolean
返回 destType = 的GetType String
结束 功能

公共 覆盖 函数 ConvertTo(context As ITypeDescriptorContext,culture As CultureInfo,value As Object ,destType As Type)作为 对象
Dim fi As FieldInfo = _enumType.GetField([ Enum ]。GetName(_enumType,value))
Dim dna As DescriptionAttribute = DirectCast (Attribute.GetCust omAttribute(fi, GetType (DescriptionAttribute)),DescriptionAttribute)

如果 dna IsNot 没什么 然后
< span class =code-keyword>返回 dna.Description
否则
返回 value.ToString()
结束 如果
结束 功能

公共 覆盖 功能 CanConvertFrom(context As ITypeDescriptorContext ,srcType As Type) As Boolean
返回 srcType = GetType String
结束 功能

公共 覆盖 功能 ConvertFrom( context As ITypeDescriptorContext,culture As CultureInfo,value As 对象作为 对象
对于 每个 fi As FieldInfo < span class =code-keyword>在 _enumType.GetFields()
Dim dna As D. escriptionAttribute = DirectCast (Attribute.GetCustomAttribute(fi, GetType (DescriptionAttribute)),DescriptionAttribute)

如果(dna IsNot Nothing AndAlso DirectCast (value, String )= dna.Description)然后
返回 [枚举]。解析(_enumType,fi.Name)
结束 如果
下一步
返回 [枚举]。解析(_enumType, DirectCast (值, Strin g ))
结束 功能
结束





显示订单是:



c

a

b



但它应该是:



a

b

c





在此先感谢,

Daniel

解决方案

覆盖 Comparer 属性 [ ^ ]具有自定义 IComparer 实例,可根据需要进行排序。



这样的东西应该有用:

  EnumDescriptionComparer:实现 IComparer 
私有 ReadOnly _enumType 作为键入

Public Sub ByVal enumType 作为类型)
_enumType = enumType
结束 Sub

私有 功能 GetDescription( ByVal value 作为 对象 As 字符串
Dim fi As FieldInfo = _enumType.GetField([ Enum ]。GetName(_enumType,value))
Dim dna As DescriptionAttribute = DirectCast (Attribute.GetCustomAttribute(fi, GetType (DescriptionAttribute)),DescriptionAttribute)
返回 如果(dna Nothing ,value.ToString(),dna.Description)
结束 功能

公开 功能比较( ByVal x As 对象 ByVal y As 对象作为 整数 Implements IComparer.Compare
Dim xDescription As 字符串 = GetDescription(x)
Dim yDescription 作为 字符串 = GetDescription(y)
返回 StringComparer.OrdinalIgnoreCase.Compare (xDescription,yDescription)
结束 功能
结束

DescriptionConverter:继承 EnumConverter
私有 ReadOnly _enumType 作为键入
私有 < span class =code-keyword> ReadOnly _comparer 作为 IComparer

公开 Sub ByVal type As Type)
MyBase New (type)
_enumType = type
_comparer = new EnumDescriptionComparer(type)
结束 Sub

受保护覆盖 ReadOnly 属性 Comparer As IComparer
获取
返回 _comparer
结束 获取
结束 属性

...
结束


除了 Richard Deeming的解决方案1 [ ^ ] ,我建议阅读:在您的枚举中添加说明 [< a href =http://www.codeproject.com/Articles/13821/Adding-Descriptions-to-your-Enumerationstarget =_ blanktitle =New Window> ^ ]。即使本文使用c#代码,您也可以将其转换为vb.net。


Hi,

How can I order the enum values in propertygrid by descripion?

Public Class myObject
    Public Enum eFolderOrder

        <ComponentModel.Description("c")> l= 0
        <ComponentModel.Description("a")> m= 1
        <ComponentModel.Description("b")> n= 2
    End Enum


    <TypeConverter(GetType(DescriptionConverter))> <System.ComponentModel.DisplayName("MyProp")> Public Property test As eFolderOrder = eFolderOrder.Description
End Class



Class DescriptionConverter
    Inherits EnumConverter
    Private _enumType As Type
    ''' <summary />Initializing instance</summary />
    ''' <param name=""type"" />type Enum</param />
    ''' this is only one function, that you must
    ''' change. All another functions for enums
    ''' you can use by Ctrl+C/Ctrl+V
    Public Sub New(type As Type)
        MyBase.New(type)
        _enumType = type
    End Sub

    Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destType As Type) As Boolean
        Return destType = GetType(String)
    End Function

    Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destType As Type) As Object
        Dim fi As FieldInfo = _enumType.GetField([Enum].GetName(_enumType, value))
        Dim dna As DescriptionAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)

        If dna IsNot Nothing Then
            Return dna.Description
        Else
            Return value.ToString()
        End If
    End Function

    Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, srcType As Type) As Boolean
        Return srcType = GetType(String)
    End Function

    Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
        For Each fi As FieldInfo In _enumType.GetFields()
            Dim dna As DescriptionAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)

            If (dna IsNot Nothing) AndAlso (DirectCast(value, String) = dna.Description) Then
                Return [Enum].Parse(_enumType, fi.Name)
            End If
        Next
        Return [Enum].Parse(_enumType, DirectCast(value, String))
    End Function
End Class



The shown order is:

c
a
b

but it supposed to be:

a
b
c


Thanks in advance,
Daniel

解决方案

Override the Comparer property[^] with a custom IComparer instance that sorts as you want it to.

Something like this should work:

Class EnumDescriptionComparer : Implements IComparer
    Private ReadOnly _enumType As Type
    
    Public Sub New(ByVal enumType As Type)
        _enumType = enumType
    End Sub
    
    Private Function GetDescription(ByVal value As Object) As String
        Dim fi As FieldInfo = _enumType.GetField([Enum].GetName(_enumType, value))
        Dim dna As DescriptionAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)
        Return If(dna Is Nothing, value.ToString(), dna.Description)
    End Function
    
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Dim xDescription As String = GetDescription(x)
        Dim yDescription As String = GetDescription(y)
        Return StringComparer.OrdinalIgnoreCase.Compare(xDescription, yDescription)
    End Function
End Class

Class DescriptionConverter : Inherits EnumConverter
    Private ReadOnly _enumType As Type
    Private ReadOnly _comparer As IComparer
    
    Public Sub New(ByVal type As Type)
        MyBase.New(type)
        _enumType = type
        _comparer = new EnumDescriptionComparer(type)
    End Sub
    
    Protected Override ReadOnly Property Comparer As IComparer
        Get
            Return _comparer
        End Get
    End Property
    
    ...
End Class


In addition to solution 1 by Richard Deeming[^], i'd recommend to read this: Adding Descriptions to your Enumerations[^]. Even if this article uses c# code, you're able to convert it to vb.net.


这篇关于如何在Propertygrid中使用dexcription对枚举值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆