问题
I've done a few WPF projects now and see the same problem pop up and that is the "problem" of aggregating/combining dependency properties (dp's).
For example, I have 10 dp's of type bool that I want to combine and expose as a seperated dp. The combined dp is true unless one or more its constituents is false.
I currently do this using the addValueChanged which registers a callback for each of the 10 (!() dp's but am wondering if there are more elegant solutions or maybe a framework that addresses this common scenario. I haven't used reactiveUI and mvvm light yet but I'm thinking it can be useful here.
var dpd = DependencyPropertyDescriptor.FromProperty(property,
owner.GetType());
dpd.AddValueChanged(owner,
handler);
回答1:
In ReactiveUI this is:
// etc all the way to ten
this.WhenAny(x => x.PropOne, x => PropTwo, x => x.PropThree,
(one,two,three) => one && two && three)
.Subscribe(x => FinalProp = x);
回答2:
DependencyProperties work based on a regular property.
Every time one of the root, regular bool properties is setted you can raise OnPropertyChanged for the "combined" bool property aswell.
The getter for the combined bool property should evaluate the root bool properties.
回答3:
Not sure it is more elegant but for each single dep prop (1...10) you have a setter.
Inside each Set
you can call (with Caliburn Micro syntax)
OnPropertyChanged(() => depPropN); // where N = 1 ..... 10
OnPropertyChanged(() => combinedDepProp);
来源:https://stackoverflow.com/questions/14196302/combine-dependency-properties