mvvm - ReactiveCommand CanExecute reacting to changes in a collection -
mvvm - ReactiveCommand CanExecute reacting to changes in a collection -
i have reactivecollection filled items (that reactiveobjects well).
i want create reactivecommand should enabled when any of items in collection has property set true, like:
mycommand = reactivecommand.create( watch items in collection see if item.myprop == true )
so anytime there 1 of items property set true, command should enabled.
edit: thanks, paul. resulting code this:
public mainviewmodel() { items = new reactivelist<itemviewmodel> { new itemviewmodel("engine"), new itemviewmodel("turbine"), new itemviewmodel("landing gear"), new itemviewmodel("wings"), }; items.changetrackingenabled = true; var shouldbeenabled = items.createderivedcollection(x => x.isadded); var shouldrecheck = observable.merge( // when items added / removed / whatever shouldbeenabled.changed.select(_ => unit.default), // when of bools in coll alter shouldbeenabled.itemchanged.select(_ => unit.default)); // kick off check on startup shouldrecheck = shouldrecheck.startwith(unit.default); clearcommand = reactivecommand.create(shouldrecheck.select(_ => shouldbeenabled.any(x => x))); }
edit 2: i've discovered trap! if modify line:
new itemviewmodel("engine");
and set isadded = true this
new itemviewmodel("engine") { isadded = true };
… when run button disabled when application starts , it should enabled. seems look doesn't evaluate after alter occurs. how can solve it?
edit 3: solved! @paul-betts says, can solved adding line:
// kick off check on startup shouldrecheck = shouldrecheck.startwith(unit.default);
the code sample update above (and in answer, too).
how this
mysourcecollection.changetrackingenabled = true; shouldbeenabled = mysourcecollection.createderivedcollection(x => x.myprop); var shouldrecheck = observable.merge( // when items added / removed / whatever shouldbeenabled.changed.select(_ => unit.default), // when of bools in coll alter shouldbeenabled.itemchanged.select(_ => unit.default)); // kick off check on startup shouldrecheck = shouldrecheck.startwith(unit.default); mycmd = reactivecommand.create(shouldrecheck.select(_ => shouldbeenabled.all(x => x));
mvvm reactive-programming reactiveui
Comments
Post a Comment