When developing Xamarin Forms applications there are some converters that we use very often. In the following article will do a compilation of the most useful converters I have used, so that you can easily use them in your projects.
(In case you are not familiar with converters you can check this article).
Here the list of converters that will be covering:
- InverseBoolConverter
- ToUpperConverter
- ToLowerConverter
- EmptyValueToBoolConverter
- NumberToCurrencyConverter
- CollectionHasElementsConverter
- DoubleToRoundedValueConverter
- RelativeDateTimeConvertor
Let’s start!
Converting a bool value to !bool value
This is definitely the most useful converter, I can say I have used it in 99% of the projects. Basically it allows you to convert a bool value to the opposite value.
Converting string to upper case string
Great converter when you want to convert a simple string text to an upper case text, so instead of creating a new property in your model to do this, you can do it by adding this converter.
Converting string to lower case string
Almost the same as the upper case converter, but it converts to lower case.
Converting Empty string to bool
Very useful when you want to show a value according to if a string is empty or not.
Converting Number to Currency
You can use it when you want to convert a number to a currency value. For example:
I won’t talk too much about it since I took the code from this great article by Vicente Guzman.
Converting a collection into bool if there is data
Normally when using a list is we create a new property in the ViewModel (HasData) to show/hide the list.
This converter returns true/false according to if the collection has elements.
Converting a double to a rounded value
Converting Date to Relative date
The last one was a converter made by Xamboy It converts any DateTime into a relative date.
I know there are some libraries that already do this, but it is an option if you want to do something simple to customize the text.
That’s all for now, you can check the full sample with all these converters here. If I missed any good one let me know.
Happy coding! 🙂
3 Comments
Hi i am kavin, its my first time to commenting anywhere,
when i read thijs article i tought i could also create comment due to
this brilliant piece of writing.
Nice
Great information about converters in xamarin forms