Update derived property in WPF with MVVM

Viewed 775

What pattern can be used to ensure a property gets updated when in the UI when it concatenates multiple sources.

For example I have a string property for the window title. It presents the application name (const string), the assembly version (readonly string), and an instance property of a type that gets loaded based on user input.

Is there a way to make the title property subscribe to the instance property so that when the instance is loaded the title automatically updates?

Right now when the recipe is loaded it updates the title property. But I'd like to reverse this so that the the recipe doesn't know about the title. It simply broadcasts that it is loaded, then anything that needs to react to a recipe being loaded would handle the event in isolation.

What design pattern is suited for this?

3 Answers

But I'd like to reverse this so that the the recipe doesn't know about the title. It simply broadcasts that it is loaded, then anything that needs to react to a recipe being loaded would handle the event in isolation.

Sounds like Steven's Cleary Calculated Properties is what you need: https://github.com/StephenCleary/CalculatedProperties

I already answered similar question in more details at https://stackoverflow.com/a/41444180/1553641

This library is magic. In fact I would recommend it for any MVVM project whether it is new or old, it is trivial to adopt the calculated properites incrementally and enjoy instant benefits.

Related