■ ListBox 클래스의 ItemsSource 속성에서 IEnumerable 객체 바인딩 에러 발생시 대처하는 방법을 보여준다.
• ListBox 클래스 ItemsSource 속성에 바인딩할 객체 타입이 아래와 같이 정의되어 있다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
/// <summary> /// 고객 컬렉션 /// </summary> public class CustomerCollection : ObjectCollection { ... } /// <summary> /// 고객 /// </summary> public class Customer : INotifyPropertyChanged { /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; ... /// <summary> /// 사진 리스트 /// </summary> private List<Photo> photoList; ... /// <summary> /// 사진 리스트 /// </summary> public List<Photo> PhotoList { get { return this.photoList; } set { this.photoList = value; OnPropertyChanged("PhotoList"); } } ... /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="propertyName">속성명</param> protected void OnPropertyChanged(string propertyName) { if(PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } /// <summary> /// 사진 /// </summary> public class Photo { ... } |
• 실버라이트 응용 프로그램을 개발하는 경우 List<T>나 Collection<T>와 같은 IEnumerable 객체를 바인딩하는 경우가 많다.
• ListBox 클래스와 Collection 타입의 속성이 자주 바인딩하고 빠른 화면 전환이 발생하는 경우 아래와 같은 에러가 자주 발생한다.
• 위와 같은 예외가 OnPropertyChanged 메소드에서 발생한다.
• 아래와 같이 속성 정의를 수정한다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/// <summary> /// 사진 리스트 /// </summary> public List<Photo> PhotoList { get { return this.photoList; } set { if(this.photoList != null) { this.photoList.Clear(); this.photoList = null; } this.photoList = value; OnPropertyChanged("PhotoList"); } } |