Combine dependency properties

拟墨画扇 提交于 2019-12-24 11:13:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!