IValueConverter Example

WPF and Silverlight extensively use XAML and data binding. Sometimes, such scenarios come up where we need to bind a control to a property but the property value needs to be converted to something which makes sense for the control. That's where IValueConverter comes in.
We will try to create a Windows Phone app where if you enter a number between 1 to 7, a textbox will show the weekday names. 1 is Monday and 7 is Sunday. Let's take a look at the xaml first.
<phone:PhoneApplicationPage.Resources>
        <src:DayNumConverter x:Key="DayConverterKey"/>
    </phone:PhoneApplicationPage.Resources>
    
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel>
                <TextBlock Text="Enter a number in the range of 1 to 7"/>
                <TextBox x:Name="tbDayNum"/>
                <TextBlock x:Name="tb" Text="{Binding Path=Text, ElementName=tbDayNum, Converter={StaticResource DayConverterKey}}"/>
            </StackPanel>
        </Grid>
    </Grid>
Here, first up, we have created a static resource and assigned it a key. Then we bind the textblock's text property to textbox's text property and apply a converter to it. The code for converter  class is shown below:

public class DayNumConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "No Day Selected";

            string str = value.ToString();
            if (string.IsNullOrEmpty(str))
                return "No Day Selected";

            int num = 0;
            if (Int32.TryParse(str, out num))
            {
                switch (num)
                {
                    case 1:
                        return "Monday";
                    case 2:
                        return "Tuesday";
                    case 3:
                        return "Wednesday";
                    case 4:
                        return "Thursday";
                    case 5:
                        return "Friday";
                    case 6:
                        return "Saturday";
                    case 7:
                        return "Sunday";
                    default:
                        return "Not a valid number";
                }
            }
            else
            {
                return "Not a valid number";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
This class does nothing but implements the IValueConverter interface. This means that everytime binding will take place, the value will be passed to this converter class before binding it to control's value. If you run the app you will see the following results: