Simplifying Bindable Properties with Type Converters in Xamarin Forms

A Type Converter allows you to convert values from a type to a different type, this is helpful when creating bindable properties in Xamarin Forms because it saves you a lot of time and validation code.

In this article, I’m going to show you a few use cases where you can use it.

NOTE: If you don’t know what’s a bindable property I recommend that you check this first.

Connecting Two Views

Have you ever wondering how the Xamarin Forms Carousel View API connects the carrousel with the indicators in XAML? Just by setting an X:Name to the IndicatorView control and assigning it to the IndicatorView property of the CarouseView.

Behind the scenes it is connecting those views through an x:Name by using a ReferenceTypeConverter.

If we don’t use it when trying to connect them, you will get the following error:

To use it you just have to create a new property in your control and set the attribute [TypeConverter(typeof(ReferenceTypeConverter))] to that property.

Ex.

If you want to see other examples of connecting two views using ReferenceTypeConverter, you can check this great post by XamValentine, in which he created a link previewer and connected two views by using it. Also, you can check the CarouselView code.

Using the XF NamedSize in your own FontSize properties

When creating a Bindable Property for setting font size in a custom view, what we usually do is creating a double property.

Which does the work, but what happens if we want to set values from the Xamarin Forms NamedSize enum like this:

We will get the following errors because that value is not double:

To make it work, you just need to add the [TypeConverter(typeof(FontSizeConverter))] attribute to your property and it will handle the conversion for you.

Providing a list of values in your XAML properties

Have you ever wondering how to define a Bindable Property that defines multiple options?

Ex.

The first thing that comes to mind is creating a converter that converts that string to a list or doing the conversion when getting the value, but that is not necessary, there is a predefined type converter for it calledListStringTypeConverter.

The code:

So, now when setting the values in the property you will get them as a list:

Creating your own type converter

In case you can’t find any predefined type converter that fits your requirements you can create your own.

Ex. Let’s create an Enum:

Let’s say we want a bindable property that supports setting an Int or an Enum value, so if the user set “Option1”, it should return 1.

An easy way to do this is by defining a TypeConverter, that converts the Enum to an Int:

Add your TypeConverter attribute to your property:

Now you can set your property as an Enum or int :).

That’s all for now, if you want to learn more you can check the full list of TypeConverters available.

The full source code of this sample can be found here.

Happy coding!

You may also like

1 Comment

  1. So much to learn from your blogs, I always wondered how ‘FontSize’ takes value from both double and ‘NamedSize’ enum, now I got it, Thank you so much.