我想在我的用户控件中添加List(Of Book)属性。我为CollectionEditor定义了Book类和BookCollectionEditor类。我还为我的用户控件定义了一个名为BookList的公共属性。对于自定义控件,它工作正常,但对于用户控制,设计器不显示我的属性。在标记上我可以添加一个Book项,但在设计器上它会出错:“Type'System.Web.UI.UserControl'没有名为'BookList'的公共属性。”
是不是可以为用户控件定义列表属性?
<TypeConverter(GetType(ExpandableObjectConverter)), Serializable()> _
Public Class Book
Private _name As String
Private _author As String
Public Sub New()
Me.New(String.Empty, String.Empty)
End Sub
Public Sub New(ByVal name As String, ByVal author As String)
_name = name
_author = author
End Sub
<Category("Book Property"), Description("Name of the Book"), DefaultValue(""), NotifyParentProperty(True)> _
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
<Category("Book Property"), Description("Author of the Book"), DefaultValue(""), NotifyParentProperty(True)> _
Public Property Author() As String
Get
Return _author
End Get
Set(ByVal value As String)
_author = value
End Set
End Property
End Class
Public Class BookCollectionEditor
Inherits CollectionEditor
Public Sub New(ByVal newType As Type)
MyBase.new(newType)
End Sub
Protected Overrides Function CanSelectMultipleInstances() As Boolean
Return False
End Function
Protected Overrides Function CreateCollectionItemType() As Type
Return GetType(Book)
End Function
End Class
Partial Class BooksUserControl
Inherits System.Web.UI.UserControl
Private _booklist As New List(Of Book)
<Category("Behavior"), _
Description("Book List"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
NotifyParentProperty(True), _
PersistenceMode(PersistenceMode.InnerProperty), _
Editor(GetType(BookCollectionEditor), GetType(BookCollectionEditor)), _
Browsable(True)> _
Public ReadOnly Property BookList() As List(Of Book)
Get
If _booklist Is Nothing Then
_booklist = New List(Of Book)()
End If
Return _booklist
End Get
End Property
End Class
Default.aspx的
<uc1:BooksUserControl ID="BooksUserControl1" runat="server">
<BookList>
</BookList>
</uc1:BooksUserControl>
答案 0 :(得分:0)
是的,可以定义List(Of T)属性。尝试使用没有编辑器设置的代码来查看问题是否存在于代码的那一部分。