VB.NET控件设计器不显示包含类属性问题求助
WinForms自定义TextBox接口属性无法在设计器展开的解决方案
问题原因
WinForms设计器默认仅解析具体类类型的属性成员,对于接口类型的属性,设计器无法识别其背后的具体实现类结构,因此即使标记了<Browsable(True),展开后也不会显示任何属性。
解决方案
方案1:将属性类型改为具体实现类(推荐)
如果业务场景允许放弃接口类型的抽象,直接将属性声明为具体实现类的类型,设计器会自动识别并展开其属性:
修改自定义TextBox类的属性定义:
Public Class TextBox Inherits Windows.Forms.TextBox Implements IBoundControl(Of String) Implements ICustomControl <Browsable(True), Category(CAT)> Public Property Bound As Binding(Of String) Implements IBoundControl(Of String).Bound <Browsable(True), Category(CAT)> Public Property Custom As Custom Implements ICustomControl.Custom Public Sub New() Me.Bound = New Binding(Of String)("Text", Me) Me.Custom = New Custom(Me) End Sub End Class
方案2:保留接口类型,添加可展开类型转换器
若必须保留接口抽象,需通过TypeConverter特性告诉设计器如何解析接口背后的具体实例属性:
- 给TextBox类的接口属性添加
TypeConverter特性:
<Browsable(True), Category(CAT), TypeConverter(GetType(ExpandableObjectConverter))> Public Property Bound As IBinding(Of String) Implements IBoundControl(Of String).Bound <Browsable(True), Category(CAT), TypeConverter(GetType(ExpandableObjectConverter))> Public Property Custom As ICustom Implements ICustomControl.Custom
- 给具体实现类
Custom也添加该特性,并补充无参构造函数:
<TypeConverter(GetType(ExpandableObjectConverter))> Public Class Custom Implements ICustom <Browsable(True), Category(CAT), DefaultValue(XAlign.None)> Public Property HorzAlign As XAlign Implements ICustom.HorzAlign Get Return _HorizAlign End Get Set(value As XAlign) _HorizAlign = value Me.CustomControl.Location = Me.CreatePoint End Set End Property <Browsable(True), Category(CAT), DefaultValue(YAlign.None)> Public Property VertAlign As YAlign Implements ICustom.VertAlign Get Return _VertAlign End Get Set(value As YAlign) _VertAlign = value Me.CustomControl.Location = Me.CreatePoint End Set End Property ' 设计器序列化需要无参构造函数 Public Sub New() End Sub Public Sub New(control As Control) Me.CustomControl = control ' 原有初始化逻辑 End Sub '.... other methods and properties End Class
额外注意事项
- 确保具体实现类的所有需要在设计器显示的属性都标记了
<Browsable(True) - 修改完成后需重新生成项目,再打开设计器查看效果
内容的提问来源于stack exchange,提问作者Karlton Kemerait
相关产品推荐
相关产品推荐

