Requirement 1
Let’s say I have some view model with two DateTime properties, Start and End. I would like for End to be set to some offset from Start, and for this to be maintained even as Start changes. This is easy enough by listening to the property notification for Start and then setting End.
Requirement 2
Now, let’s add the fun part: I want to be able to stop adjusting End if the user explicitly changes it. Put another way, let’s say it’s an airline reservation system, and we want to set the end date to 3 days later (based on the average length of stay), but if the user explicitly sets the end date to a specific date, then when he/she sets the start date again, the end date stays).
The Problem
On first thought, it would seem you could just listen for End‘s notification and set some value that the handler will see when handling Start, and say “ok, I won’t adjust End. The problem is that End doesn’t know why it is being changed, so the handler for End‘s change will blithely set the stop-changing condition immediately. This violates requirement #1.
How can I satisfy both requirements?
Let’s say we have a flag
AutoAdjustthat tells you if you should adjustEndwhenStartchanges.AutoAdjustis initiallytrue.Whenever
Endis set, if the interval betweenStartandEndis different than the “default” interval, setAutoAdjusttofalse.Whenever
Startis set, ifAutoAdjustistruethen adjust the value ofEnd.Note that with this scheme, it will never be possible to set
AutoAdjustback totrueonce it flips tofalse. This is intentional, and IMHO more intuitive than allowingAutoAdjustto flip back — the user can easily reason that the end date has stopped auto-adjusting because they manually modified it, but it’s not as intuitive to think that if they set it back to its “auto” value it will start auto-adjusting again.