Hi,
Im working with PropertyGrid, and one of my properties is Enum. I want use the description of enum value as DisplayMember and value as ValueMember
It worked me on winform combobox, but I try it in EditorRequired event, and nothing happends.
editor = New PropertyGridDropDownListEditorelement = CType(editor.EditorElement, BaseDropDownListEditorElement)element.ValueMember = "Value"element.DisplayMember = "Description"element.DataSource = Tools.GetEnumValueDescription(Of MyEnum)()e.Editor = editorWhen I click in that enum property, values I can select are the same as I dont use that code.
What I am doing wrong?
Thank you!
3 Answers, 1 is accepted
Thank you for writing.
I can suggest handling the EditorInitialized event of the control for the enum item. Then in the event handler, you can subscribe the editor element to the VisualItemFormatting event and change the text of the items displayed in the popup. Please check the following documentation for additional information: https://docs.telerik.com/devtools/winforms/propertygrid/editors/handling-editors'-events.
I hope this helps. Let me know if you have other questions.
Regards,
Hristo
Progress Telerik
Hello Hristo,
I have been trying with VisualItemFormmating but I dont know how to do. I explain my case.
That is my enum
Public Enum TestEnum <Description("A")> a <Description("B")> b <Description("C")> c <Description("D")> dEnd EnumI have this class for wrap enum value and description
Public Class EnumValueDescription#Region "Propiedades"#Region "Value" Private _Value As [Enum] Public Property Value() As [Enum] Get Return _Value End Get Set(ByVal value As [Enum]) _Value = value End Set End Property#End Region#Region "Description" Private _Description As String Public Property Description() As String Get Return _Description End Get Set(ByVal value As String) _Description = value End Set End Property#End Region#End RegionEnd ClassThis code converts enum to that list of objects:
Public Function GetEnumValueDescription(Of T)() As List(Of EnumValueDescription) Try 'Checekea si el enum es correcto If (Not GetType(T).IsEnum) Then Throw New ArgumentException("T debe ser un enumerador") End If 'Obtiene los valores y descripciones del enum Return [Enum].GetValues(GetType(T)).Cast(Of [Enum])().Select(Function(value) New EnumValueDescription With {.Value = value, .Description = CType(Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), GetType(DescriptionAttribute)), DescriptionAttribute).Description}).ToList() Catch ex As Exception Throw New Exception(String.Format("Error obteniendo los valores del enum '{0}'.{1}{2}", GetType(T).Name, vbNewLine, ex.Message), ex) End Try End Function
Doing it in winform combobox I just do that:
combobox.ValueMember = "Value"combobox.DisplayMember = "Description"combobox.DataSource = Tools.GetEnumValueDescription(Of TestEnum)()
Using EditorRequired event, with properties that are not enums my aproach works good with seting list(of myobj) and setting valuemember and displaymember, but doesnt work with enum properties. I try with EditorInitialized and doesnt works :/.
Can you give me and example of code please?
Thank you very much!
In the VisualItemFormatting event of the editor element, you have access to the items displayed in the list and you can alter their text. The code snippet you have provided demonstrates better your local setup and actually a similar to your initial approach could work. Instead of handling the EditorRequired event you will need to handle the EditorInitialized event where you can change the data source of the existing editor. It will be also necessary to sync the currently selected index. I am assuming that you will also need to change the text of the value element so you will also need to handle the ItemFormatting event of the property grid. Please check my code snippet below:
Public Class RadForm1 Sub New() InitializeComponent() Me.ComboBox1.ValueMember = "Value" Me.ComboBox1.DisplayMember = "Description" Me.ComboBox1.DataSource = Me.GetEnumValueDescription(Of TestEnum)() Me.RadDropDownList1.ValueMember = "Value" Me.RadDropDownList1.DisplayMember = "Description" Me.RadDropDownList1.DataSource = Me.GetEnumValueDescription(Of TestEnum)() Me.RadPropertyGrid1.SelectedObject = New PropertyGridDataModel With { .Id = 1, .Name = "John Doe", .TestEnum = TestEnum.A } AddHandler Me.RadPropertyGrid1.EditorInitialized, AddressOf RadPropertyGrid1_EditorInitialized AddHandler Me.RadPropertyGrid1.ItemFormatting, AddressOf RadPropertyGrid1_ItemFormatting End Sub Private Sub RadPropertyGrid1_ItemFormatting(sender As Object, e As PropertyGridItemFormattingEventArgs) If TypeOf (TryCast(e.Item, PropertyGridItem)).Value Is TestEnum Then Dim value = DirectCast(TryCast(e.Item, PropertyGridItem).Value, TestEnum) DirectCast(e.VisualElement, PropertyGridItemElement).ValueElement.Text = Me.GetSingleEnumValueDescription(value) Else e.VisualElement.ResetValue(LightVisualElement.TextProperty, Telerik.WinControls.ValueResetFlags.Local) End If End Sub Private Sub RadPropertyGrid1_EditorInitialized(sender As Object, e As PropertyGridItemEditorInitializedEventArgs) If TypeOf (TryCast(e.Item, PropertyGridItem)).Value Is TestEnum Then Dim editor = CType(e.Editor, PropertyGridDropDownListEditor) Dim element = CType(editor.EditorElement, BaseDropDownListEditorElement) Dim index = element.SelectedIndex element.DataSource = Nothing element.ValueMember = "Value" element.DisplayMember = "Description" element.DataSource = Me.GetEnumValueDescription(Of TestEnum)() element.SelectedIndex = index End If End Sub Public Function GetEnumValueDescription(Of T)() As List(Of EnumValueDescription) Try 'Checekea si el enum es correcto If (Not GetType(T).IsEnum) Then Throw New ArgumentException("T debe ser un enumerador") End If 'Obtiene los valores y descripciones del enum Return [Enum].GetValues(GetType(T)).Cast(Of [Enum])().Select(Function(value) New EnumValueDescription With {.Value = value, .Description = CType(Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), GetType(DescriptionAttribute)), DescriptionAttribute).Description}).ToList() Catch ex As Exception Throw New Exception(String.Format("Error obteniendo los valores del enum '{0}'.{1}{2}", GetType(T).Name, vbNewLine, ex.Message), ex) End Try End Function Public Function GetSingleEnumValueDescription(value As TestEnum) As String Try Dim attributes As DescriptionAttribute() = CType(value.GetType().GetField(value.ToString()).GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute()) Return attributes(0).Description Catch ex As Exception Throw New Exception(String.Format("Error obteniendo los valores del enum '{0}'.{1}{2}", GetType(TestEnum).Name, vbNewLine, ex.Message), ex) End Try End FunctionEnd ClassPublic Class EnumValueDescription#Region "Value" Private _Value As [Enum] Public Property Value() As [Enum] Get Return _Value End Get Set(ByVal value As [Enum]) _Value = value End Set End Property#End Region#Region "Description" Private _Description As String Public Property Description() As String Get Return _Description End Get Set(ByVal value As String) _Description = value End Set End Property#End RegionEnd ClassPublic Enum TestEnum <Description("A Desc")> A <Description("B Desc")> B <Description("C Desc")> C <Description("D Desc")> DEnd EnumPublic Class PropertyGridDataModel Public Property Id As Integer Public Property Name As String Public Property TestEnum As TestEnumEnd ClassI am also attaching a short video showing the result on my end.
Regards,
Hristo
Progress Telerik
