Enumeration Mapping – Implementation

Now that your enumeration types are correctly mapped to the Entity Data Model, you can start taking full advantage of this feature. In this section, I’ll explain how to simply work with entity types that contain enumeration type properties, but most importantly how to bind them to items controls and display their values in a specific language by using resource files.

First of all, let’s start with a classic scenario: you want to display all values of your enumeration in a list in order to let the user select one. Since those values are static, you don’t have to connect to the service to retrieve them. Just start by creating a property of type System.Array in your ViewModel. In the getter of this property, you can retrieve all values of your enumeration type by using the GetValues static method of the Enum type, as shown in Figure 1.

05 ViewModel Property

Figure 1 – ViewModel Property

Thanks to this simple code snippet, you can already bind your newly created property to any items control. But the displayed values may not be very user-friendly. Indeed, they are simple string conversions of the enumeration values. But don’t worry, you don’t need to implement any translation system yourself as there is already one available in the Keep It Soft framework. Before being able to use it, you have to create an entry for each enumeration value in your language resource files. Be sure to respect the Keep It Soft Projects Conventions for enumerations, as shown in Figure 2.

04 Language Resources

Figure 2 – Language Resources

You now have everything you need to easily translate the values of your enumeration type. For this purpose, you can simply call the GetTranslation method of the EnumHelper class, which is located in the KeepItSoft.Fwk.Helpers namespace. While this can be manually done in any layer, some other helpers also exist in the user interface to include translation to your binding. For instance, if you want to bind a property like the one we declared earlier to a ComboBox, you can use the EnumToTextTemplate resource, as shown in Figure 3. This template will call the helper method of the framework to automatically display the translation of each enumeration value.

06 ComboBox Binding

Figure 3 – Enum ItemTemplate

Aside from the template, the same automatic translation system is also offered through a converter called EnumToResourceStringConverter. A code sample with the use of both the template and the converter is shown in Figure 4.

07 Column Binding

Figure 4 – Enum Converter