我一直在試圖將一個標籤綁定到只讀屬性。我有一個更復雜的項目,我正在實施它,它不工作。我一直未能獲得幫助,所以我創建了一個更簡單的項目版本,並且我的數據綁定仍然沒有更新。數據綁定不更新
要複製我的問題,您將需要一個帶有文本框,標籤和按鈕的窗體,然後是一個類。
該類的代碼如下
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> List> arrayList>(0)> System.Windows.Forms.Binding> DataSource> Databinding_test.databinding中查看細節類(包括hnumber的正確值)被列出,所以對我來說,這表明標籤實際上知道它應該綁定的值。
有人可以請我填寫我失蹤的事情,使這一切工作,因爲它幾乎使我拔出我所有的頭髮。
感謝, MTG