c# - Observe for ItemChanged on two ReactiveLists -
i have viewmodel in listen changes items in 2 reactivelists, payments , accountpayments. lists instantiated , changetrackingenabled set true:
this.payments = new reactivelist<transaction.paymentviewmodel>(); this.payments.changetrackingenabled = true;` this.accountpayments = new reactivelist<accountpaymentviewmodel>(); this.accountpayments.changetrackingenabled = true;` then, defined in viewmodel, have observableaspropertyhelper readonly property:
readonly observableaspropertyhelper<decimal> _totalpayments; public decimal totalpayments { { return _totalpayments.value; } } my intent set totalpayments whenever of these items change in both lists. tried using whenany:
this.whenany(vm => vm.payments.itemchanged, vm => vm.accountpayments.itemchanged, (a, b) => a.sender.payments .where(x => x.amount.hasvalue).sum(x => x.amount.value) + b.sender.accountpayments .where(x => x.amount.hasvalue).sum(x => x.amount.value)) .toproperty(this, vm => vm.totalpayments, out _totalpayments); while compiles fine, doesn't seem catch changes. tried using whenanyobservable:
this.whenanyobservable( vm => vm.payments.itemchanged, vm => vm.accountpayments.itemchanged) .select(_ => this.payments.where(x => x.amount.hasvalue) .sum(x => x.amount.value) + this.accountpayments.where(x => x.amount.hasvalue) .sum(x => x.amount.value)) .toproperty(this, vm => vm.totalpayments, out _totalpayments); but won't compile. there way accomplish trying do? appreciated.
the first won't work it's observing property changes , itemchanged won't change, it's observable.
the second pretty correct, requires bit of modification. whenanyobservable requires observables same type. you're uninterested in actual result, can select unit , merge two:
this.whenanyobservable(a => a.payments.itemchanged).select(_ => unit.default) .merge(this.whenanyobservable(a => a.accountpayments.itemchanged).select(_ => unit.default)); you can't select unit.default within whenanyobservable re-writes expression observe property changes make sure has latest observable. if neither payments nor accountpayments change (i.e. they're read only), can omit whenanyobservable altogether:
payments.itemchanged.select(_ => unit.default) .merge(accountpayments.itemchanged.select(_ => unit.default));
Comments
Post a Comment