数据绑定到具有一个或多个可选参数的对象参数
我有一个名为“PERSON”的对象,
该对象有一个标题、名字和名字。姓氏财产以及许多其他目前无关紧要的财产。它还具有一个名为 Fullname 的只读属性,该属性根据调用 PERSON.FULLNAME 时传递的可选参数“withTitles”连接上述两个或三个参数
PERSON.FULLNAME(true) <- 如果有任何标题,将添加标题
PERSON.FULLNAME(false) <- 将给出不带标题的名称
Public ReadOnly Property FullName(Optional ByVal withTitle As Boolean = False) As String
Get
Dim _ttle As String = Me.Title
Select Case withTitle
Case True
If _ttle.Length > 0 Then _ttle += " " Else _ttle = String.Empty
Case False
_ttle = String.Empty
End Select
If Me.FirstName <> "" And Me.LastName <> "" Then
Return _ttle & Me.FirstName & " " & Me.LastName
ElseIf Me.FirstName = "" And Me.LastName <> "" Then
Return _ttle & Me.LastName
ElseIf Me.FirstName <> "" And Me.LastName = "" Then
Return _ttle & Me.FirstName
ElseIf Me.FirstName = "" And Me.LastName = "" Then
Return Me.ContactName
End If
End Get
End Property
当我尝试将 PERSONCOLLECTION(PERSON 对象的集合)绑定到RadioButtonList 或任何其他绑定控件。
RadioButtonList1.DataSource = _personCollection
RadioButtonList1.DataTextField = "FullName"
RadioButtonList1.DataValueField = "ContactID"
RadioButtonList1.DataBind()
我收到错误:PERSON 不包含“FullName”属性。如果我将其更改为任何其他不带参数的属性,它将按预期工作。
现在我猜测绑定过程无法处理对象属性的可选或强制参数,对吗?有更好的方法吗?
我考虑过循环遍历集合来手动添加它们,但这有点破坏了数据绑定的对象!
任何帮助将不胜感激。 凯夫。
I have an object, called 'PERSON'
This person object has a title, firstName & Surname property as well as many other which are at the moment irrelevant. It also has a read only property called Fullname which concatenates the two or three parameters mentioned above depending on an optional parameter 'withTitles' passed over when you call PERSON.FULLNAME
PERSON.FULLNAME(true) <- Will add titles if there are any
PERSON.FULLNAME(false) <- Will give the name without the title
Public ReadOnly Property FullName(Optional ByVal withTitle As Boolean = False) As String
Get
Dim _ttle As String = Me.Title
Select Case withTitle
Case True
If _ttle.Length > 0 Then _ttle += " " Else _ttle = String.Empty
Case False
_ttle = String.Empty
End Select
If Me.FirstName <> "" And Me.LastName <> "" Then
Return _ttle & Me.FirstName & " " & Me.LastName
ElseIf Me.FirstName = "" And Me.LastName <> "" Then
Return _ttle & Me.LastName
ElseIf Me.FirstName <> "" And Me.LastName = "" Then
Return _ttle & Me.FirstName
ElseIf Me.FirstName = "" And Me.LastName = "" Then
Return Me.ContactName
End If
End Get
End Property
My problem surfaces when I try to bind my PERSONCOLLECTION (a collection of the PERSON object) to a RadioButtonList or any other binding control at that.
RadioButtonList1.DataSource = _personCollection
RadioButtonList1.DataTextField = "FullName"
RadioButtonList1.DataValueField = "ContactID"
RadioButtonList1.DataBind()
I get an error: PERSON does not contain a property of 'FullName'. If I change this to any other property that does not take a parameter it works as expected.
Now I'm guessing that the binding procedure can't handle optional or mandatory parameters for object properties, is this right? Is there a better way to do it?
I thought about looping through the collection to add them manually but that kinda defeats the object of DataBinding!
Any help would be apreciated.
Kev.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议创建两个属性,
FullName
和FullNameWithTitle
。尽管属性可以采用参数,但它们旨在作为对象的索引器,而不是可操作的值。例如,表示Color
对象集合的类可能具有Item
属性,其中可选索引是要返回的特定颜色。I would recommend creating two properties,
FullName
andFullNameWithTitle
. Although properties can take parameters they are intended to be indexers into the object and not actionable values. For instance a class that represents a collection ofColor
objects might have anItem
property with an optional index being the specific color to return.