2009-11-10 32 views
1

如何更改时发布的读取属性已经改变了TotalPublicationsRead的价值?C# - 如何更改属性的基础上改变他人财产(的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财产和忘记的事件。在下面的代码我只是计算列表,其中Publication一直Read的项目。

的方式,你试图做到这一点,你就必须有当ObserableCollection改变的事件处理程序。然后,你就必须将事件处理程序附加到PropertyChanged事件,会增加或减少TotalPublicationsRead财产。我确信它会起作用,但它会变得更加复杂。

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; } 
    } 
}