VB.NET数据绑定标签未更新

时间:2014-01-06 09:57:57

标签: vb.net data-binding

我遇到了带有数据绑定文本值更新标签的问题。我有一个计算运动员最佳投掷的分类,这与我的表格中的标签绑定。该课程如下

Imports System.ComponentModel
Imports System.Runtime.CompilerServices

Public Class Competition
    Public Sub New()
        Competitors = New List(Of Competitor)()
    End Sub
    Public Property Competitors() As List(Of Competitor)
        Get
            Return m_Competitors
        End Get
        Set(value As List(Of Competitor))
            m_Competitors = value
        End Set
    End Property
    Private m_Competitors As List(Of Competitor)
    Public ReadOnly Property CurrentPlacings() As List(Of Competitor)
        Get
            Return Competitors.OrderByDescending(Function(c) c.BestThrow).ToList()

        End Get
    End Property

End Class
Public Class Competitor
    Implements System.ComponentModel.INotifyPropertyChanged
    Public Event PropertyChanged(sender As Object,
            e As System.ComponentModel.PropertyChangedEventArgs) _
            Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    Public Sub New()
        Throws = New List(Of [Throw])()
    End Sub
    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set(value As String)
            m_FirstName = value
        End Set
    End Property
    Private m_FirstName As String
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set(value As String)
            m_LastName = value
        End Set
    End Property
    Private m_LastName As String
    Public Property compNumber() As String
        Get
            Return m_compNumb
        End Get
        Set(value As String)
            m_compNumb = value
        End Set
    End Property
    Private m_compNumb As String
    Public Property club() As String
        Get
            Return m_club
        End Get
        Set(value As String)
            m_club = value
        End Set
    End Property
    Private m_club As String
    Public Property Throws() As List(Of [Throw])
        Get
            Return m_Throws
        End Get
        Set(value As List(Of [Throw]))
            m_Throws = value
        End Set
    End Property
    Private m_Throws As List(Of [Throw])
    Public ReadOnly Property BestThrow() As Object
        Get
            Dim bt = Throws.Where(Function(t) t.Status = ThrowStatus.Valid).OrderByDescending(Function(t) t.Distance).First()
            If (IsNothing(bt.Distance)) Then
                bt.Distance = "0"
            End If
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("BestThrow"))
            Return bt

        End Get
    End Property
    Public ReadOnly Property getLabel
        Get
            Return compNumber & " " & LastName & ", " & FirstName & vbCrLf & club
        End Get
    End Property

End Class
Public Enum ThrowStatus
    Valid
    Pass
    Foul
End Enum
Public Class [Throw]
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Property Status() As ThrowStatus
        Get
            Return m_Status
        End Get
        Set(value As ThrowStatus)
            m_Status = value
        End Set
    End Property
    Private m_Status As ThrowStatus
    Public Property Distance() As String
        Get
            If Status = ThrowStatus.Valid Then
                If (m_Distance > 0) Then
                    Return m_Distance
                Else
                    Return Nothing
                End If

            ElseIf Status = ThrowStatus.Pass Then
                Return "PASS"
            ElseIf Status = ThrowStatus.Foul Then
                Return "FOUL"
            Else
                Return Nothing
            End If

        End Get
        Set(value As String)
            If (value > 0) Then


                If (IsNumeric(value)) Then
                    m_Distance = value
                    Status = ThrowStatus.Valid
                ElseIf (value = "foul") Then
                    Status = ThrowStatus.Foul
                ElseIf (value = "pass") Then
                    Status = ThrowStatus.Pass
                Else
                    Status = ThrowStatus.Valid
                    m_Distance = Nothing
                End If
            Else
                m_Distance = Nothing
                Status = ThrowStatus.Valid
            End If
            OnPropertyChanged("Distance")
        End Set
    End Property
    Protected Sub OnPropertyChanged(ByVal name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub
    Private m_Distance As Decimal
    Public Property attempt() As Integer
        Get
            Return m_attempt
        End Get
        Set(value As Integer)
            m_attempt = value
        End Set
    End Property
    Private m_attempt As Integer

End Class

标签数据绑定的行如下:


best.DataBindings.Add(New Binding("text", athlete.BestThrow, "distance", False, DataSourceUpdateMode.OnPropertyChanged))

我可以说,Property BestThrow使用Watch进行了更新,但由于某种原因,标签似乎只反映了Throw(0).Distance而不是BestThrow.Distance

我可以改变Throw(0)并且标签会改变,如果我在其他5次尝试中添加更大的数字并观察值我可以看到BestThrow正在更新。

提前感谢您的协助。

标记

1 个答案:

答案 0 :(得分:0)

我认为你有一个错误的预测,属性BestThrow永远不会更新,因为它没有setter。当您使用watch检查BestThrow值时,会重新计算'因为您请求了值。您需要的是在BestThrow添加或删除项目时为Throws添加属性更改事件的方法,因为BestThrow值取决于Throws。事件发生后,UI将再次调用BestThrow's getter查找更新的值。

'You need to execute this code to notify UI that `BestThrow` value has changed
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("BestThrow"))

Raisng事件可以在用于向/ Throws属性添加/删除项目的代码之后手动完成,或者您可以使Throws成为Throw的ObservableCollection,然后收听Collection Changed事件。然后在事件处理程序中,您可以引发BestThrow属性更改事件。