0

ここには、多かれ少なかれ互いに依存しているクラスがいくつかあります。リレーションシップは、依存関係ツリーのようなものです。

class A {
  List<B> _bs = new List<B>();

  public int ValueOfA { 
    get {
      return _bs.Sum(p => p.ValueOfB);
    }
}

class B {
  List<C> _cs = new List<C>();
  public int ValueOfB {
    get {
      return _cs.Where(p => p.ValueOfC > 1).Sum(p => p.ValuOfC);
    }
  }

class C {
  public int ValueOfC { get; set }
}

_bsそのため、_csまたは変更されるたびに、ValueOfCそれらに関連するすべてのプロパティも変更されたことが通知され、再計算される必要があります。

この目標を一貫して確実に達成するための最良の方法は何ですか? これを自動的に行う方法はありますか?

4

1 に答える 1

0

INotifyPropertyChangedクラスで実装する必要がありますC。のセットでValueOfC、イベントを発生させます。

class C : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int valueOfC;
    public int ValueOfC
    {
        get { return valueOfC; }
        set
        {
            valueOfC = value;
            OnPropertyChanged(PropertyChanged);
        }
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventHandler handler)
    {
        if (handler != null)
            handler(this, new PropertyChangedEventArgs("ValueOfC"));
    }
}

私はちょうどそれをテストしました、そしてそれは完全に動作します。

保護された仮想メソッドにイベントを発生させることは、一般的な方法です。

補足として、リストが変更された場合に何かをしたい場合は、BindingListまたはObservableCollectionの使用を検討することをお勧めします。

編集

ツリー全体をリフレッシュする小さな例を作成しました。

public class Bank : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private BindingList<Customer> customers = new BindingList<Customer>();
    public int Worth
    {
        get { return customers.Sum(cust => cust.FullBalance); }
    }

    public Bank()
    {
        customers.ListChanged += new ListChangedEventHandler(customers_ListChanged);
    }

    void customers_ListChanged(object sender, ListChangedEventArgs e)
    {
        Console.WriteLine("A customer has changed.");
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs("Worth"));
    }

    public void Add(Customer c) { customers.Add(c); }
}

public class Customer : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private BindingList<Account> accounts = new BindingList<Account>();
    public int FullBalance
    {
        get { return accounts.Sum(acc => acc.Balance); }
    }

    public Customer()
    {
        accounts.ListChanged += new ListChangedEventHandler(accounts_ListChanged);
    }

    void accounts_ListChanged(object sender, ListChangedEventArgs e)
    {
        Console.WriteLine("An account has changed.");
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs("FullBalance"));
    }

    public void Add(Account a) { accounts.Add(a); }
}

public class Account : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int balance = 0;
    public int Balance
    {
        get { return balance; }
        set
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs("Balance"));
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Account a1 = new Account() { Balance = 5 };
        Account a2 = new Account() { Balance = 10 };
        Account a3 = new Account() { Balance = 15 };

        Customer c1 = new Customer(); c1.Add(a1); c1.Add(a2);
        Customer c2 = new Customer(); c2.Add(a3);

        Bank b = new Bank(); b.Add(c1); b.Add(c2);

        Console.WriteLine();

        a1.Balance += 100;
    }
}

if (e.ListChangedType == ListChangedType.ItemChanged)これで、イベント ハンドラのようなもの、または類似のものを書くことができます。

于 2013-02-10T14:08:19.517 に答える