VB.NET编程语言的推出为开发者又增加了一种语言的选择。他们可以利用这一款语言实现各种特定的功能。VB.NET数据绑定能应用于控件的任何属性。我看到过很多人提到能够绑定文本框的背景颜色到数据项,举个例子,超期的帐号的背景色显示红色。
但是如果你试图使用数据集或者数据表实现该功能,将会遇到问题。数据行只能保持受到限制的数据类型,并且不支持Color类型。如果你不能把颜色存储在数据中怎么能绑定颜色呢?
有些途径可以解决这个问题,但是最简单的是用VB.NET数据绑定到自定义数据对象代替绑定到数据表。自定义业务对象的属性可能是Color型的,这样的属性能绑定到控件的BackColor属性。
为了演示,我定义了下面的自定义事务对象:
Public Class Account
Dim m_nAccountID As Integer
Dim m_sCustomerName As String
Dim m_dblBalance As Double
Public Sub New(ByVal nAccountID
As Integer, ByVal sCustomerName
As String, _ByVal dblBalance As Double)
Me.AccountID = nAccountID
Me.CustomerName = sCustomerName
Me.Balance = dblBalance
End Sub
Public Property AccountID() As Integer
Get
Return m_nAccountID
End Get
Set(ByVal Value As Integer)
m_nAccountID = Value
End Set
End Property
Public Property CustomerName() As String
Get
Return m_sCustomerName
End Get
Set(ByVal Value As String)
m_sCustomerName = Value
End Set
End Property
Public Property Balance() As Double
Get
Return m_dblBalance
End Get
Set(ByVal Value As Double)
m_dblBalance = Value
End Set
End Property
Public ReadOnly Property
BackColor() As Color
Get
If m_dblBalance < 0 Then
Return Color.Salmon
Else
Return SystemColors.Window
End If
End Get
End Property
End Class
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
注意只读的BackColor属性从Balance属性中得到值,并且为负平衡(negative balance)暴露了一个不同的颜色。该类的其它元素很直接。
VB.NET数据绑定的相关应用技巧就为大家介绍到这里。
【编辑推荐】