IValueConverter in WPF data binding

One of the handy things that you can do with data binding in WPF is that you convert the data as you pull it from the data source. The mechanism for this is the IValueConverter interface.
Let's say that you have a list of numbers representing positions in a race, and you want to display this list with the appropriate positional text (for example, 1 is 1st, 2 is 2nd, 3 is 3rd, etc). Rather than save your data in this form (which would not be convenient at all) you could leave the data as an int in the data source, and write a value converter to convert the integer into the appropriate string. This has many benefits - for example, if the UI is being virtualized, then only the items that have been created will be converted. And if the position changes WPF takes care of updating the string.
The first step is to make a class deriving from IValueConverter. We will call our class PositionConverter. There are two interface members - one of them to convert to the target type, and one of them to convert back. We don't need to convert back, so we just need to implement ConvertTo:

public class PositionConverter : IValueConverter


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)


            if (value != null)


                int number = (int)value;


                string numberText = number.ToString();

                string positionText;


                if (number >= 10 && numberText[numberText.Length - 2] == '1')


                    // teen numbers always end in 'th'

                    positionText = "th";




                    switch (numberText[numberText.Length - 1])


                        case '1':

                            positionText = "st";


                        case '2':

                            positionText = "nd";


                        case '3':

                            positionText = "rd";



                            positionText = "th";





                return (numberText + positionText);



            return string.Empty;



        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)


            throw new Exception("The method or operation is not implemented.");



To use the converter we can create one in the resources section of the window, and refer to it as a static resource in the binding statement:

<Window x:Class="ValueConverterDemo.Window1"








    <local:PositionConverter x:Key="PositionConverter" />


  <ListBox Name="numberBox">



        <TextBlock Text="{Binding Converter={StaticResource PositionConverter}}" />





Now the numbers show up with positional text in them (45 becomes 45th, for example).

Value converters are very useful tools for letting you use the power of data binding. You can even pass parameters to them, so that you can combine data in the value converter.

Next up I show what ConvertBack is used for.

Comments (6)
  1. Anonymous says:

    Is there a bug when using htis on Vista?  When I do something similar I get an error saying that ‘The element Window.Resources in namespace http://schemas.microsoft… has some invalid child elemment PositionConverter…

    And the error is on this portion of the XAML:



    [BenCon – if you post the full contents of the XAML file then I might be able to help you diagnose things. The xmlns attribute on the root element is very important.

     Also, errors in VS are not as meaningful as compiler errors. Which is this?]

  2. Anonymous says:

    This is great, I’m trying to add something like this to generic.xaml in Themes subdirectory as I am working on a custom control.

    Only problem is: how do I add the Windows.Resources section to that? Or do I add it to the window(s) hosting the control?

  3. BenCon says:

    ZiggyShort –

    You don’t need to add the converter to the Window.Resources. You just need the converter to be in a place visible to the binding expression that you use it in.

  4. sleekgeek says:

    Is there an easier way of doing this for trivial cases?

    For example, if you had a Label binding to a string property and all you wanted to do was perform a .ToLower().

    Can we do this through fancy binding syntax or markup extensions? It seems creating a new Converter class for trivial cases is a bit overkill.

Comments are closed.

Skip to main content