MVVM - binding to aggregated property

假装没事ソ 提交于 2019-12-02 07:06:43

The data binding approach seems easiest in this particular case. That way you rely upon WPF's data binding mechanism handling the event subscriptions for you, rather than having to do it manually via code.

The only other way I know of handling this is to use some kind of mediator object. When the Valid property of SubItem is changed you send out a message that this has happened. Your Item class, which has subscribed to this message, handles it by checking the current valid state of A and B, then sets its own Valid property accordingly.

This approach is not without its own wrinkles however. For one thing, you need to inject the mediator object into your ViewModel objects. Also your Item objects need to subscribe to and unsubscribe from the relevant message at the appropriate times (usually on object creation and destruction). All of this plumbing, while still easier than handling property changed events directly, is more difficult than just using a data trigger and relying on WPF's binding mechanism IMO.

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