我有一个DataRepeater1,Label1和Button1在ItemTemplate上。这三个控件绑定到一个BindingList(Of T),其中T是atm,这是一个非常简单的类,具有单个字符串属性。
当用户单击DataRepeater项的一个按钮时,它会更新绑定数据列表中的字符串。也就是说,如果用户单击DataRepeater中项0上的按钮,则相同索引处的BindingList中的字符串将被更改。
这行得通
不起作用的是在字符串更改之后,DataRepeater应该更新相关项的Label1,因为它绑定到该字符串--但它没有。
有人能告诉我为什么吗?我现在的代码在下面。谢谢
Imports System.ComponentModel
Public Class Form1
Class ListType
Public Sub New(newString As String)
Me.MyString = newString
End Sub
Public Property MyString As String
End Class
Dim MyList As New BindingList(Of ListType)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Bind BindingList to DataRepeater.
Label1.DataBindings.Add("Text", MyList, "MyString")
DataRepeater1.DataSource = MyList
' Add some items to the BindingList.
MyList.Add(New ListType("First"))
MyList.Add(New ListType("Second"))
MyList.Add(New ListType("Third"))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Use the Index of the current item to change the string
' of the list item with the same index.
MyList(DataRepeater1.CurrentItemIndex).MyString = "Clicked"
' Show all the current list strings in a label outside of
' the DataRepeater.
Label2.Text = String.Empty
For Each Item As ListType In MyList
Label2.Text = Label2.Text & vbNewLine & Item.MyString
Next
End Sub
End Class发布于 2013-09-05 13:09:22
嗯,这似乎很奇怪,但经过一些进一步的实验,我发现,与其直接更改字符串,不如创建对象的副本,更改字符串,并使相应索引处的对象与副本相等。
例如:
Dim changing As ListType = MyList(DataRepeater1.CurrentItemIndex)
changing.MyString = "Clicked"
MyList(DataRepeater1.CurrentItemIndex) = changing或者更短的版本:
MyList(DataRepeater1.CurrentItemIndex).MyString = "Clicked"
MyList(DataRepeater1.CurrentItemIndex) = MyList(DataRepeater1.CurrentItemIndex)似乎BindingList只在整个对象发生变化时才通知DataRepeter,而不是对象的成员.
发布于 2013-11-28 20:59:47
看看INotifyPropertyChanged
INotifyPropertyChanged接口用于通知客户端(通常是绑定客户端)属性值已更改。
正是使用这种机制,BindingList类能够将单个对象的更新推送到DataRepeater。
如果您在T中实现了T接口,则会通知BindingList对T的任何更改,并将它们转发给您的DataRepeater。只要更改了属性(在PropertyChanged属性的设置程序中),就必须引发T事件。
.NET框架文档对于使用这种方法有一个演练。
https://stackoverflow.com/questions/18636747
复制相似问题