数据绑定不更新

时间:2014-02-16 14:18:25

标签: vb.net winforms data-binding

我一直试图将数据绑定到readonly属性。我有一个更复杂的项目,我正在实现它,它无法正常工作。我没有成功获得帮助,所以我创建了一个更简单的项目版本,我的数据绑定仍然没有更新。

要复制我的问题,您需要一个带有文本框,标签和按钮的表单,然后是一个类。

该课程的代码如下


Imports System.ComponentModel
Public Class databinding
    Implements INotifyPropertyChanged
    Public Sub New()
        numbers = New List(Of number)
    End Sub
    Public Property numbers As List(Of number)
        Get
            Return m_number
        End Get
        Set(value As List(Of number))
            m_number = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("hnumber"))
        End Set
    End Property
    Private m_number As List(Of number)
    Public ReadOnly Property hnumber As Integer
        Get
            Dim list As IList(Of number) = (From t As number In numbers Select t Order By t.value Descending).ToList()
            If (list.Count > 0) Then
                If (IsNothing(list(0).value)) Then
                    Return "0"
                Else
                    Return list(0).value
                End If

            End If
            Return "0"
        End Get

    End Property

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class
Public Class number
    Public Property value As Integer
        Get
            Return t_number
        End Get
        Set(value As Integer)
            t_number = value
        End Set
    End Property
    Private t_number As Integer
End Class

表格的代码如下:


Public Class Form1
    Public numberlist As New databinding
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.DataBindings.Add(New Binding("text", numberlist, "hnumber"))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newnum As New number
        newnum.value = TextBox1.Text
        numberlist.numbers.Add(newnum)
    End Sub
End Class

现在根据我的理解当您单击按钮时,文本框中的数字会添加到此列表中,这会发生,并且hnumber值会更新,使用断点和手表我也可以看到。从阅读开始,我需要实现inotifypropertychanged,当我设置新数字以获取标签以重新检查数据绑定(已经完成)。

然而标签将保持为0.如果我通过Label1运行监视,我可以在DataBindings>下看到它。列表> arrayList> (0)> System.Windows.Forms.Binding> DataSource> Databinding_test.databging类的详细信息(包括hnumber的正确值)被列出,所以对我来说,这表明Label确实知道它应该绑定的值。

有人可以请我填写我所缺少的内容,以使这一切都能正常工作,因为这几乎导致我把所有的头发拉出来。

谢谢, MTG

1 个答案:

答案 0 :(得分:1)

我之前试图向你解释这一点,我会再次。

未更新绑定的原因是您要将值添加到列表中。

numberlist.numbers.Add(newnum)

但是,如果您“更改”列表,则会触发propertychanged事件。

numberlist.numbers.Add(newnum)
numberlist.numbers = numberlist.numbers '<--

您应使用IList<T>代替使用ObservableCollection<T>,以便跟踪所做的更改。

Public Class databinding
    Implements INotifyPropertyChanged

    Public Sub New()
        Me.numbers = New ObservableCollection(Of number)
    End Sub

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Property numbers As ObservableCollection(Of number)
        Get
            Return m_number
        End Get
        Set(value As ObservableCollection(Of number))
            If (Not m_number Is value) Then
                Unhook(m_number)
                Hook(value)
                m_number = value
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("hnumber"))
            End If
        End Set
    End Property

    Public ReadOnly Property hnumber As Integer
        Get
            If (Not numbers Is Nothing) Then
                Dim list As IList(Of number) = (From t As number In numbers Select t Order By t.value Descending).ToList()
                If (list.Count > 0) Then
                    If (IsNothing(list(0).value)) Then
                        Return 0
                    Else
                        Return list(0).value
                    End If
                End If
            End If
            Return 0
        End Get
    End Property

    Private Sub Hook(collection As ObservableCollection(Of number))
        If (Not collection Is Nothing) Then
            AddHandler collection.CollectionChanged, AddressOf Me.OnNumbersChanged
        End If
    End Sub

    Private Sub OnNumbersChanged(sender As Object, e As NotifyCollectionChangedEventArgs)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("hnumber"))
    End Sub

    Private Sub Unhook(collection As ObservableCollection(Of number))
        If (Not collection Is Nothing) Then
            RemoveHandler collection.CollectionChanged, AddressOf Me.OnNumbersChanged
        End If
    End Sub

    Private m_number As ObservableCollection(Of number)

End Class