【发布时间】:2020-03-20 00:15:54
【问题描述】:
如何在属性网格中拥有作为对象(不仅仅是字符串)下拉列表的属性?我走了这么远,但后来卡住了!考虑下面的代码:
Public Class mOrganisation
Public Property ID as String
Public Property Name as String
End Class
Public Class mSystem
Public Property ID as string
Public Property Name as String
Public Property Developer as mOrganisation
Public Overrides Function ToString() As String
Return Name
End Function
End Class
Public Class mGame
Public Property ID as string
Public Property Name as String
<TypeConverter(GetType(SystemConverter))>
Public Property System as mSystem
End Class
Public Class Main
Public Systems as List(of mSystem) = [...list gatehring code here]
End Class
Public Class SystemConverter
Inherits TypeConverter
Public Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
Return False
End Function
Public Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As TypeConverter.StandardValuesCollection
Return New StandardValuesCollection(Main.Systems)
End Function
End Class
mOrgnisation 只是为了给 mSystem 类引入一些复杂性。现在这段代码确实会下拉值:
但是当我选择一个值时,我得到一个 PropertyGrid 错误“'System.String' 类型的对象无法转换为'mSystem' 类型”
这让我陷入了困境,特别是尝试应用 Convert From 和 Convert To 的各种排列。但是,我找不到合适的解决方案。通过ConvertFrom 进行的一次尝试使下拉菜单加载非常缓慢,一次一个项目(我认为它正在为每个项目触发)。
我会制作一个自定义 UITypeEditor,但我找不到像标准下拉菜单那样获得 PropertyGrid 固有调整大小方法/句柄的方法(并尝试编写我自己的调整大小例程,但我认为它被证明是粘性和闪烁的,因为PropGrid+控件的交互)
实现这一目标的最佳/最优雅的方式是什么?
【问题讨论】:
标签: .net propertygrid typeconverter uitypeeditor