C# WPF Value Converters

Continuing with the example of the text box and the slider we are going to improve the program by ensuring that at most two decimal places are displayed in the text box. For a more complete example, see the post RGB Colour Viewer.


A data converter is a special class you write that intercepts the data between the source and the target and that can manipulate it however you want. A data converter class must use the ValueConversion attribute and must implement the IValueConverter interface. The interface consists of two methods — Convert and ConvertBack.

<Window x:Class="TwoWayConverter.MainWindow"
        Title="Converter" Height="150" Width="360">
        <TextBox Margin="10">
                <Binding ElementName="sldrSlider" Path="Value">
                        <!--  Above: Associate Converter with the Binding 
                        Below: Notice that you need to add the namespace of the
                        project to use the data converter class and then
                        associate the converter with the binding.  -->
        <Slider Name="sldrSlider" TickPlacement="TopLeft" Margin="10"/>

The following data converter class, called DisplayTwoDecPlaces, is in a separate file called DisplayTwoDecPlaces.cs. The namespace of this project, in this particular case, is TwoWayConverter.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;  // add this for IValueConverter

namespace TwoWayConverter
    // You must specify the ValueConversion attribute in square brackets
    //           type of the source,type of the target
    [ValueConversion(typeof(double), typeof(string))]
    public class DisplayTwoDecPlaces  : IValueConverter
        public object Convert(object value, Type targetType,
                object parameter, System.Globalization.CultureInfo culture)
            double dValue = (double)value;
            return dValue.ToString("F2");
        public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
            double dValue;
            double.TryParse((string)value, out dValue);
            return dValue;


If you want to use a .NET namespace that is not a XAML namespace, you must use a special syntax to tell the XAML parser you want to use a .NET namespace. .NET namespaces are called CLR namespaces. To specify a CLR namespace called, TwoWayConverter, which is declared in your project, you would use the following syntax.


For a namespace in your project, the prefix local is often used. Notice that the string being assigned to the prefix starts with the string clr-namespace:. This keyword string tells the XAML parser that the string needs to be parsed rather than being used as the name of a XAML namespace.

If the namespace is in another assembly, the process is similar, but you must also include the assembly name—without the .dll extension.


Leave a comment

Your email address will not be published. Required fields are marked *