В конце концов отказался от TypeConverter и нашел решение. Как и большинство кошмаров кодирования, это были 5 простых строк, помещенных в нужное место! Ключом был доступ к встроенному дескриптору изменения размера PropertyGrid. Вы можете настроить код ниже, чтобы включить построение в обработку пользовательских объектов, так как value
- любой объект. Однако для примера ниже приведена строка, для простоты:
Public Overrides ReadOnly Property IsDropDownResizable As Boolean
Get
Return True
End Get
End Property
Вот как это сделать:
Шаг 1: Создайте новый UITypeEditor
Это связывает свойство с пользовательским редактором. значение содержит значение, переданное из свойства. Это может быть объект любого типа. Поэтому вы можете передать его в свой пользовательский элемент управления, чтобы сообщить в него любые функции редактирования.
Создайте новый класс:
Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.Windows.Forms.Design
Public Class TestUIEditor
Inherits UITypeEditor
' Indicate that we display a dropdown.
Public Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) _
As UITypeEditorEditStyle
Return UITypeEditorEditStyle.DropDown
End Function
Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
' Get an IWindowsFormsEditorService object.
Dim editor_service As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
If editor_service Is Nothing Then
Return MyBase.EditValue(context, provider, value)
End If
'Setup the editor
Dim editorService As IWindowsFormsEditorService = Nothing
If provider IsNot Nothing Then
editorService = TryCast(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
End If
If editorService IsNot Nothing Then
Dim testUI As New TestPropGridUI(editorService)
'Populate the existing value to the dropdown control
testUI.Text = value
'Drop down the control
editorService.DropDownControl(testUI)
'Update property value once dropdown closed
value = testUI.Text
End If
Return value
End Function
Public Overrides ReadOnly Property IsDropDownResizable As Boolean
Get
'Ensures control is resizable
Return True
End Get
End Property
End Class
Шаг 2: Создайте свой собственный редактор:
В этом примере я только что использовал пользовательский класс, который наследует TextBox. Однако вы можете использовать любые элементы управления, даже пользовательский UserControl.
Imports System.Windows.Forms.Design
Public Class TestPropGridUI
Inherits TextBox
' The editor service displaying this control.
Private m_EditorService As IWindowsFormsEditorService
Public Sub New(ByVal editor_service As IWindowsFormsEditorService)
MyBase.New()
m_EditorService = editor_service
Dock = DockStyle.Fill
End Sub
Public Sub returnPressed(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
'Closes the dropdown when Enter pressed
If e.KeyCode = Keys.Enter Then
m_EditorService.CloseDropDown()
End If
End Sub
End Class
Примечание m_EditorService.CloseDropDown. Поместите его туда, где вы хотите, чтобы раскрывающийся список закрывался сам (например, после выбора значения).
Шаг 3. Скажите свойству класса использовать пользовательский редактор:
Public Class TestObject
Public Property ID as String
Public Property Quantity as Integer
<Editor(GetType(TestUIEditor), GetType(UITypeEditor))>
Public Property TestText As String
End Class
И это все!