MVVM update of calculated properties

前端 未结 5 662
粉色の甜心
粉色の甜心 2020-12-17 21:54

I\'m just learning MVVM, and I\'m trying to work how to display changes to a calculated property as a result of changes to the values from which it\'s calculated. All of the

5条回答
  •  粉色の甜心
    2020-12-17 22:35

    The solution most often offered here seems to be to get all of the properties on which the tax value depends to invoke PropertyChanged in the ModelView for the property itself and for every property that depends on it. ....

    Yes, but only for that object: Each property should fire its own property change event in the setter. Additionally the setter should somehow trigger properties that depend on that value on itself. You should not try to trigger updates on other objects proactively: they should listen to this objects PropertyChanged.

    The best solution I can think of is to make the class that does the calculation receive PropertyChanged events, and raise a new PropertyChanged event for the tax value when anything changes that goes into the calculation. That at least preserves encapsulation at class level, but it still breaches method encapsulation: the class shouldn't have to know about how a method does its work.

    This is indeed the standard way. Each class has the responsibility to monitor the properties that it depends on, and fire property change events for the properties it.

    There are probably frameworks that will help to do this for you, but it is worthwhile to know what should happen.

提交回复
热议问题