I’m still trying to get my head around MVVM so apologies if this question is basic.
My program displays some text on screen, but depending on the status of another property will depend on the color of the text.
In my mind, I could do this in 2 ways.
-
When the Status property is changed, the color is changed from within the setter. EG (Pseudo code)
private LogDetails.LogStatus? logStatus; public LogDetails.LogStatus? LogStatus { get { return logStatus; } set { Color color = //logic to work out the color Color = color; } } -
I think this can be done via the OnPropertyChanged (which I can research myself).
My question is, since I am given 2 options to complete my simple task, is one better than the other in certain situations, is one more depreciated/old or am I forced to use option 2 just to appease the MVVM pattern?
I would assume that if I eventually require 2 way binding (so I can update the status from a GUI for example) then the MVVM is better, but as it stands today, does any one have any thoughts?
You should simply write a custom
IValueConverterthat takes theLogStatusand translates it to a color. Bind theTextdirectly toLogStatusand bind theColorof the control to theLogStatususing the converter.The code is really simple (just implement one method, add the converter as a XAML resource and reference it in your binding), but you can see a similar example here.