Click here to Skip to main content
15,881,139 members
Articles / Mobile Apps
Tip/Trick

Using Integer Instead of Enum in XAML

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
26 Sep 2015CPOL1 min read 17.2K   64   6  
Using integer instead of Enum for ComboBox and Radio Button in XAML

Introduction

It is common programming practice to use Enumerations to make code more readable but it also is annoying when saving to database since you always have to convert it back to integer or other type depending on case. This tip is about to use Integer directly instead of using Enum in XAML.

Background

I'm working in universal App (for Windows and Windows phone) and found it is trivial to work with enumerations on project since, by default, WinRT applications do NOT have direct access to any type of database structure.

I did start using SQLite as an extension, therefore it becomes annoying using enum, as​ it requires conversion to native data types.

Using the Code

To put it in an example, first we create a Test Class:

C#
public class ClsTest : System.ComponentModel.INotifyPropertyChanged
{
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName]
                                        String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }

    private int _iCombo;
    //[SQLite.Column("iCombo")]
    public int iCombo
    {
        get { return _iCombo; }
        set { _iCombo = value; NotifyPropertyChanged("iCombo"); }
    }

    private int _iRadio;
    //[SQLite.Column("iRadio")]
    public int iRadio
    {
        get { return _iRadio; }
        set { _iRadio = value; NotifyPropertyChanged("iRadio"); }
    }
    public ClsTest()
    {

    }
}

For ComboBox, we create class to support List of integer and converter:

C#
public class ComboListInt : List<int>
{
}
public class ConverterComboIntToString : Windows.UI.Xaml.Data.IValueConverter
{
    object Windows.UI.Xaml.Data.IValueConverter.Convert
		(object value, Type targetType, object parameter, string language)
    {
        if (value != null)
        {
            int i = -1;
            i = (int)value;

            switch (i)
            {
                case 0:
                    return "First";
                case 1:
                    return "Second";
                case 2:
                    return "Third";
                case 3:
                    return "Fourth";
            }
        }
        return "";
    }
    object Windows.UI.Xaml.Data.IValueConverter.ConvertBack
		(object value, Type targetType, object parameter, string language)
    {
        return value;
    }
}

For RadioButton, we create converter:

C#
public class ConverterRadioIsChecked : Windows.UI.Xaml.Data.IValueConverter
{
    object Windows.UI.Xaml.Data.IValueConverter.Convert
		(object value, Type targetType, object parameter, string language)
    {
        if (value != null && parameter != null)
        {
            int i = -1;
            int.TryParse(parameter.ToString(), out i);
            if ((int)value == i)
            {
                return true;
            }
        }
        return false;
    }
    object Windows.UI.Xaml.Data.IValueConverter.ConvertBack
		(object value, Type targetType, object parameter, string language)
    {
        if (value != null && parameter != null)
        {
            int i = -1;
            int.TryParse(parameter.ToString(), out i);
            return i;
        }
        return -1;
    }
}

In XAML, we initialize all our classes and converters as static resources:​​

XML
<Page.Resources>
    <!--initialize ClsTest with iCombo = 2 and iRadio = 2-->
    <local:ClsTest iCombo="2" iRadio="2" x:key="myTestClass" />

    <!--List of integers for ComboBox-->
    <local:ComboListInt x:key="myComboListInt">
        <x:Int32>0</x:Int32>
        <x:Int32>1</x:Int32>
        <x:Int32>2</x:Int32>
        <x:Int32>3</x:Int32>
    </local:ComboListInt>

    <!--converter for ComboBox items-->
    <local:ConverterComboIntToString x:key="myConverterComboIntToString" />

    <!--converter for RadioButton-->
    <local:ConverterRadioIsChecked x:key="myConverterRadioIsChecked" />

</Page.Resources>

Put initialized ClsTest into DataContext of parent control (e.g.: Page, Grid, StackPanel, etc.) so its properties are available to both ComboBox and RadioButtons.

XML
<StackPanel DataContext="{StaticResource myTestClass}">
   <!--ComboBox-->
   <!--RadioButton 1-->
   <!--RadioButton 2-->
   ....
</StackPanel>

XAML for ComboBox:

XML
<ComboBox ItemsSource="{StaticResource myComboListInt}" 
          ​SelectedValue="{Binding Path=iCombo, Mode=TwoWay}">
    <ComboBox.TtemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource myConverterComboIntToString}}" />
        </DataTemplate>
    </ComboBox.TtemTemplate>
</ComboBox>

XAML for RadioButton (note integers are passed as parameters of converter and descriptions are directly in content):

XML
<RadioButton IsChecked="{Binding Path=iRadio, Converter={StaticResource myConverterRadioIsChecked}, 
	ConverterParameter=0, Mode=TwoWay}">One</RadioButton>

<RadioButton IsChecked="{Binding Path=iRadio, Converter={StaticResource myConverterRadioIsChecked}, 
	ConverterParameter=1, Mode=TwoWay}">Two</RadioButton>

<RadioButton IsChecked="{Binding Path=iRadio, Converter={StaticResource myConverterRadioIsChecked}, 
	ConverterParameter=2, Mode=TwoWay}">Three</RadioButton>

<RadioButton IsChecked="{Binding Path=iRadio, Converter={StaticResource myConverterRadioIsChecked}, 
	ConverterParameter=3, Mode=TwoWay}">Four</RadioButton>
 

Points of Interest

This can be used to work with globalization setting using language parameter of converters to match proper regional settings:

We could also have created converter to present Content of radio buttons, e.g.:

C#
public class ConverterRadioIntToString : Windows.UI.Xaml.Data.IValueConverter
{
    object Windows.UI.Xaml.Data.IValueConverter.Convert
	(object value, Type targetType, object parameter, string language)
    {
        int i = -1;
        if (parameter != null)
        { 
            int.TryParse(parameter.ToString(), out i);
            switch (i)
            {
                case 0:
                    return "Um one";
                case 1:
                    return "Dois Two";
                case 2:
                    return "Tres Tree";
                case 3:
                    return "Quatro Four";
            }
        }
        return "???";
    }
    object Windows.UI.Xaml.Data.IValueConverter.ConvertBack
	(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

in XAML resources:

XML
<local:ConverterRadioIntToString x:key="myConverterRadioIntToString" />

RadioButton XAML would be:

XML
<RadioButton Content="{Binding Converter={StaticResource myConverterRadioIntToString}, 
   ​ConverterParameter=1}" 
   ​IsChecked="{Binding Path=iRadio, 
   Converter={StaticResource myConverterRadioIsChecked}, 
   ConverterParameter=1, Mode=TwoWay}">    
</RadioButton>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Mozambique Mozambique
Trying to create start up...

Comments and Discussions

 
-- There are no messages in this forum --