首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当更改绑定数据时,DataRepeater不会更新

当更改绑定数据时,DataRepeater不会更新
EN

Stack Overflow用户
提问于 2013-09-05 12:46:07
回答 2查看 1.2K关注 0票数 0

我有一个DataRepeater1,Label1和Button1在ItemTemplate上。这三个控件绑定到一个BindingList(Of T),其中T是atm,这是一个非常简单的类,具有单个字符串属性。

当用户单击DataRepeater项的一个按钮时,它会更新绑定数据列表中的字符串。也就是说,如果用户单击DataRepeater中项0上的按钮,则相同索引处的BindingList中的字符串将被更改。

这行得通

不起作用的是在字符串更改之后,DataRepeater应该更新相关项的Label1,因为它绑定到该字符串--但它没有。

有人能告诉我为什么吗?我现在的代码在下面。谢谢

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-05 13:09:22

嗯,这似乎很奇怪,但经过一些进一步的实验,我发现,与其直接更改字符串,不如创建对象的副本,更改字符串,并使相应索引处的对象与副本相等。

例如:

代码语言:javascript
复制
    Dim changing As ListType = MyList(DataRepeater1.CurrentItemIndex)
    changing.MyString = "Clicked"
    MyList(DataRepeater1.CurrentItemIndex) = changing

或者更短的版本:

代码语言:javascript
复制
    MyList(DataRepeater1.CurrentItemIndex).MyString = "Clicked"
    MyList(DataRepeater1.CurrentItemIndex) = MyList(DataRepeater1.CurrentItemIndex)

似乎BindingList只在整个对象发生变化时才通知DataRepeter,而不是对象的成员.

票数 0
EN

Stack Overflow用户

发布于 2013-11-28 20:59:47

看看INotifyPropertyChanged

INotifyPropertyChanged接口用于通知客户端(通常是绑定客户端)属性值已更改。

正是使用这种机制,BindingList类能够将单个对象的更新推送到DataRepeater

如果您在T中实现了T接口,则会通知BindingListT的任何更改,并将它们转发给您的DataRepeater。只要更改了属性(在PropertyChanged属性的设置程序中),就必须引发T事件。

.NET框架文档对于使用这种方法有一个演练

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18636747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档