In a previous article we talked about Triggers, in which basically we were using it for use cases when we wanted to change a control value based on a property value in the model.
So instead of writing the code in our model, we do in our XAML.
Converters use a similar concept, just instead of adding the validation logic in the view it uses a different class to do the validation.
So if we want to do the same example using converters, the steps are simple:
1-Create a converter class
When creating the new class, implement the interface IValueConverter by adding two methods:
- Convert: To convert a model value to a new value to be shown.
- ConvertBack: Could be use when the value will be saved in a different format. For example you have a DateTime, and you want it to be saved with a different format, you can use this method to add the conversion.
The converter receives 4 parameters, the first one is the value to be converted, the second one the element type that is expected to be converted, the third one the parameter (you can pass an extra parameter to the converter), and the last one the culture info.
Tip: Is better to add all your converters classes in a folder in order to be organized.
Tip: For naming the converter is better to use an intuitive in name, as a personal preference I like to put a name referring to what the converter does, for example if the converter converts a null to bool with the purpose of being use on IsVisible property so is called, NullToVisibilityConverter.
2-Add the logic you want to handle in the converter
3-Add the converter to your XAML
Add the converter reference, then specify your converter with a key name on the resource dictionary and use it in the property you wish.
When to use Converters vs Triggers
These are my personal rules to decide when using one or the other.
-
Use triggers when you want to modify a lot of properties
For example, if you want to change a lot of properties of the Label according to the user role being Admin:
We cannot do it by using a simple converter. So we will have to create one converter per object return type (One returns the color, other returns text, etc)
-
Use a converter when you want to reuse a logic in a lot of places
For example, if you need to make visible a control only if the property has an opposite value (False).
Probably you will need this logic in a lot of places in your app, so if you create a converter you can use the validation logic and also the XAML is shorter.
-
Use converters when a property has a lot of possible values (For example if you have a Switch)
Imagine that according to a user role you want to change the text color of a Label.
If the user has a lot of roles, doing this with a DataTrigger generates a big XAML.
This code using converters is simpler and easier to maintain.
-
Use a converter when you want to add logic
For example, if you want to show the value according to the parameter being higher than X number + divisor by 3.
-
Use a converter when you want to change the value of a property in your binding context object
Using triggers is not possible to update a property of the object you are binding to, but using a converter this is possible thanks to the ConvertBack method.
NOTE
You can use a mix and match data triggers and converters for more complex cases, to take the best of each one.
References
That’s all for now, check the full source code here.
Happy coding!