2014-02-16 183 views
0

我一直在試圖將一個標籤綁定到只讀屬性。我有一個更復雜的項目,我正在實施它,它不工作。我一直未能獲得幫助,所以我創建了一個更簡單的項目版本,並且我的數據綁定仍然沒有更新。數據綁定不更新

要複製我的問題,您將需要一個帶有文本框,標籤和按鈕的窗體,然後是一個類。

該類的代碼如下


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

回答

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