合并依赖项属性 [英] Combine dependency properties

查看:89
本文介绍了合并依赖项属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经完成了一些WPF项目,看到了同样的问题,这就是聚合/合并依赖项属性(dp)的问题".

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).

例如,我有10个bool类型的dp,我想将它们合并并作为一个单独的dp公开.除非组合的dp的一个或多个成分为假,否则组合的dp为true.

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.

我目前使用addValueChanged进行此操作,该方法为10个(!()dp的每一个都注册了一个回调,但我想知道是否有更优雅的解决方案或解决此常见情况的框架. mvvm还不错,但是我认为它在这里很有用.

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);

推荐答案

在ReactiveUI中,这是:

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);

这篇关于合并依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆