2009-11-10 3 views
1

게시 읽기 속성이 변경되면 TotalPublicationsRead 값을 어떻게 변경합니까?- 다른 속성 (ObservableCollection)의 변경 내용을 기반으로 속성 값을 변경하려면 어떻게해야합니까?

public class Report 
{ 
    public ObservableCollection<Publication> Publications { get; set; } 
    public int TotalPublicationsRead { get; set; } 
} 

public class Publication : INotifyPropertyChanged 
{ 
    private bool read; 
    public bool Read 
    { 
     get { return this.read; } 
     set 
     { 
     if (this.read!= value) 
     { 
      this.publications = value; 
      OnPropertyChanged("Read"); 
     } 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    private void OnPropertyChanged(string property) 
    { 
     if (this.PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    }   
} 

미리 감사드립니다.

답변

4

내가 생각하는대로하려는 경우 TotalPublicationsRead 속성을 변경하고 이벤트를 잊어 버릴 수 있습니다. 아래 코드에서 나는 PublicationRead 인 목록의 항목을 계산합니다.

당신이하려고했던 방식으로 ObserableCollection이 변경되었을 때 이벤트 처리기가 있어야합니다. 그런 다음 TotalPublicationsRead 속성을 증가 시키거나 감소시킬 이벤트 처리기를 PropertyChanged 이벤트에 연결해야합니다. 나는 그것이 잘 될 것이라고 확신하지만 훨씬 더 복잡 할 것이다.

public class Report 
{ 
    public List<Publication> Publications { get; set; } 
    public int TotalPublicationsRead 
    { 
     get 
     { 
      return this.Publications.Count(p => p.Read); 
     } 
    } 

} 

public class Publication : INotifyPropertyChanged 
{ 
    private bool read; 
    public bool Read 
    { 
     get { return this.read; } 
     set { this.read = value; } 
    } 
} 
관련 문제