— If you don’t know what a converter is, I recommend you reading this article first.
Have you ever wondered how to pass multiple parameters to a converter, or how to create properties in a converter to change the value based on them? If you want to know how to achieve it then keep reading :).
Use Case
You have multiple properties (Age, IsAdmin, Name) in your ViewModel and according to these properties you want to change the TextColor of a Label. Example: If Age is >= 18 and IsAdmin is true then it should return red, if Age is < 18 and IsAdmin is true then would return green, etc.
Ideas
When you think about how to achieve it, the first thing that comes to mind is to do something like this:
Basically, having a CustomerValuesToColorConverter and creating Bindable properties for Age and IsAdmin, then bind it to the ViewModel properties.
The problem with this approach is that the converters don’t handle the property change, so if you change the Age after updating the IsAdmin, it won’t update the property value.
Solution
Thanks to Multi-Binding that scenario is possible to achieve since now we can have an IMultiValueConverter. We just need to add MultiBinding to the targeted property, specify all our bindings, and use the IMultiValueConverter.
Create a converter that extends from IMultiValueConverter, in the values parameter you will receive each value, so every time any of these values change your converter will get notified and update the converted value according to your defined conditions.
Result
That’s all for now! You can check the full sample here.
Happy coding!