1

PublicationのReadプロパティが変更された場合、TotalPublicationsReadの値を変更するにはどうすればよいですか?

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

2 に答える 2

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; }
    }
}
于 2009-11-10T12:46:16.680 に答える
0

依存関係プロパティを使用できます。

詳細については、 http : //www.wpftutorial.net/dependencyproperties.htmlhttp ://msdn.microsoft.com/en-us/library/ms745795(v=vs.110).aspxをご覧ください。

于 2014-04-16T22:26:10.267 に答える