2013-09-05 4 views
0

ItemTemplate에 Label1 및 Button1이있는 DataRepeater1이 있습니다. 이 세 개의 컨트롤은 BindingList (Of T)에 바인딩됩니다. 여기서 T는 atm이라는 단일 문자열 속성을 가진 매우 간단한 클래스입니다.바운드 데이터를 변경할 때 DataRepeater가 업데이트되지 않습니다.

사용자가 DataRepeater Item의 단추 중 하나를 클릭하면 바인딩 된 데이터의 문자열을 업데이트합니다 명부. I.E. 사용자가 DataRepeater에서 항목 0의 단추를 클릭하면 동일한 인덱스에있는 BindingList의 문자열이 변경됩니다.

이 작동하지 않습니다 무엇

문자열이 해당 문자열에 바인딩으로 관련 항목에 대한 Label1을 업데이트해야입니다 DataRepeater 변경 이후입니다 작동 -하지만하지 않습니다.

아무도 말해 줄 수 있습니까? 내 현재 코드는 아래와 같습니다. 감사합니다

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 

답변

0

는 음, 그것은 이상한 것 같지만 몇몇 더 실험 후 나는 발견 대신 복사본을 동일한 관심을 인덱스에있는 개체를 개체의 복사본을 생성, 직접 문자열을 변경 문자열을 변경하고 만드는 공장.

Dim changing As ListType = MyList(DataRepeater1.CurrentItemIndex) 
    changing.MyString = "Clicked" 
    MyList(DataRepeater1.CurrentItemIndex) = changing 

또는 짧은 버전 : 같은

어떻게 든에만 DataRepeter 통지

MyList(DataRepeater1.CurrentItemIndex).MyString = "Clicked" 
    MyList(DataRepeater1.CurrentItemIndex) = MyList(DataRepeater1.CurrentItemIndex) 

이는 바인딩 것 같다 때 전체 개체 변경이 아닌 객체의 멤버 ...

1

INotifyPropertyChanged에서보세요 :

INotifyPropertyChanged 인터페이스는 클라이언트 (일반적으로 바인딩 클라이언트)에 등록 정보 값이 변경되었음을 알리는 데 사용됩니다.

그것은 BindingList 클래스는 DataRepeater에 개별 개체에 대한 업데이트를 푸시 할 수있는이 메커니즘을 사용하고.

당신이 T에서 INotifyPropertyChanged 인터페이스를 구현하는 경우

BindingListT에 변경 사항을 통보하고 당신을위한 DataRepeater로에 전달됩니다. 속성이 변경 될 때마다 ( T의 속성의 설정자에서) PropertyChanged 이벤트를 발생 시키면됩니다.

.NET Framework 문서에는이 방법을 사용하는 경우 walkthrough이 있습니다.

+1

"이 링크는 질문에 대한 대답 일지 모르지만 여기에 답변의 핵심 부분을 포함하고 참조 용 링크를 제공하는 것이 좋습니다. 링크 된 페이지가" – zero323

+1

@ zero323 : INotifyPropertyChnaged를 사용하는 방법에 대한 해답을 업데이트했습니다. 감사. –

관련 문제