VB.Net - 将TextBox绑定到集合的属性

时间:2014-07-25 12:37:04

标签: vb.net collections binding

我有一个Account类和Accountlist集合。

Public ReadOnly Property TotalAmount() As Decimal
    Get
        If ACCOUNTList.Count > 0 Then
            Return ACCOUNTList.Sum(Function(s) s.InvoiceAmount)
        Else
            Return 0
        End If            
    End Get
End Property

正如在上面的代码中可以看到的,我喜欢将Accountlist集合中的TotalAmount属性绑定到textBox,以便在BindingSource更改textBox的值时自动更改。我可以在BindingSource中访问集合Count但我不知道如何将TotalAmount属性绑定到BindingSource!我怎样才能做到这一点?

 Private Sub ACCOUNTBindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles ACCOUNTBindingSource.ListChanged
        txtBox1.Text = ACCOUNTBindingSource.Count
        txtBox2.Text = ACCOUNTBindingSource.???

    End Sub

2 个答案:

答案 0 :(得分:1)

您的课程需要实施INotifyPropertyChanged。然后你只需要调用PropertyChanged事件。还有一个很好的例子here

答案 1 :(得分:1)

您可以将其更改为Function():

Public Function TotalAmount() As Decimal
    If ACCOUNTList.Count > 0 Then
        Return ACCOUNTList.Sum(Function(s) s.InvoiceAmount)
    Else
        Return 0
    End If            
End Function 

所以每次你使用Textbox1.Text = [Some Class] .TotalAmount()。ToString()它会处理并返回新的总和值