IValueConverter interface

IValueConverter interface

Exposes methods that allow the data to be modified as it passes through the binding engine.


public interface class IValueConverter


The IValueConverter interface has these types of members:


The IValueConverter interface has these methods. It also inherits methods from the Object class.

Convert Modifies the source data before passing it to the target for display in the UI.
ConvertBack Modifies the target data before passing it to the source object. This method is called only in TwoWay bindings.



You can create a class that allows you to convert the format of your data between the source and the target by inheriting from IValueConverter. For example, you might want to have a list of colors that you store as RGBA values, but display them with color names in the UI. By implementing Convert and ConvertBack, you can change the format of the data values as they are passed between the target and source by the binding engine.

You should always implement Convert with a functional implementation, but it's fairly common to implement ConvertBack so that it reports a not-implemented exception. You only need a ConvertBack method in your converter if you are using the converter for two-way bindings, or using XAML for serialization.

Note  To data-bind to a custom value converter that is written in Visual C++ component extensions (C++/CX), the header file in which the IValueConverter implementation class is defined must be included, directly or indirectly, in one of the code-behind files. For more info, see Create your first Windows Store app using C++.
Tip  Some of the default project templates for a Windows Store app include a helper class, BooleanToVisibilityConverter. This class is an IValueConverter implementation that handles a common custom-control scenario where you use Boolean values from your control logic class to set the Visibility value in XAML control templates.

Migration notes

In the Windows Runtime, the language parameters for IValueConverter methods use strings, as opposed to using CultureInfo objects as they do in the Windows Presentation Foundation (WPF) and Microsoft Silverlight definitions of the interface.


The following example shows how to implement the IValueConverter interface and use the converter when data binding to a collection of objects.

Important  If you are coding in C++/CX, remove the ConverterParameter attribute from the last TextBlock, because that particular string value is specific to .NET string formatting. Your entire element should look like this: <TextBlock Text="{Binding Path=ReleaseDate, Mode=OneWay, Converter={StaticResource FormatConverter}}" />.

<UserControl x:Class="ConverterParameterEx.Page"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" >
           <local:DateFormatter x:Key="FormatConverter" />
        <ComboBox Height="60" Width="250" x:Name="MusicCombo" 
                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name, Mode=OneWay}" />
                        <TextBlock Text="{Binding Path=Artist, Mode=OneWay}" />
                        <TextBlock Text="{Binding Path=ReleaseDate, Mode=OneWay,
                            Converter={StaticResource FormatConverter}, 
                            ConverterParameter=\{0:d\}}" />

// MainPage.xaml.h
// Declaration of the MainPage class.

#pragma once

#include "MainPage.g.h"

namespace IValueConverterExample

    // Simple business object.
    public ref class Recording sealed 
        Recording (Platform::String^ artistName, Platform::String^ cdName, Windows::Foundation::DateTime release)
            Artist = artistName;
            Name = cdName;
            ReleaseDate = release;
        property Platform::String^ Artist;
        property Platform::String^ Name;
        property Windows::Foundation::DateTime ReleaseDate;

    public ref class DateFormatter  sealed : Windows::UI::Xaml::Data::IValueConverter 
        // This converts the DateTime object to the Platform::String^ to display.
        virtual Platform::Object^ Convert(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType, 
            Platform::Object^ parameter, Platform::String^ language)
            Windows::Foundation::DateTime dt = safe_cast<Windows::Foundation::DateTime>(value); 
            Windows::Globalization::DateTimeFormatting::DateTimeFormatter^ dtf =
            return dtf->Format(dt); 

        // No need to implement converting back on a one-way binding 
        virtual Platform::Object^ ConvertBack(Platform::Object^ value, Windows::UI::Xaml::Interop::TypeName targetType, 
            Platform::Object^ parameter, Platform::String^ language)
            throw ref new Platform::NotImplementedException();

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public ref class MainPage sealed
            m_myMusic = ref new Platform::Collections::Vector<Recording^>();

            // Add items to the collection.

            // You can use a Calendar object to create a Windows::Foundation::DateTime
            auto c = ref new Windows::Globalization::Calendar();
            c->Year = 2008;
            c->Month = 2;
            c->Day = 5;
            m_myMusic->Append(ref new Recording("Chris Sells", "Chris Sells Live",

            c->Year = 2007;
            c->Month = 4;
            c->Day = 3;
            m_myMusic->Append(ref new Recording("Luka Abrus",
                "The Road to Redmond", c->GetDateTime()));
            c->Year = 2007;
            c->Month = 2;
            c->Day = 3;
            m_myMusic->Append(ref new Recording("Jim Hance",
                "The Best of Jim Hance", dt));

            // Set the data context for the combo box.
            MusicCombo->DataContext = m_myMusic;    

        virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;

        Windows::Foundation::Collections::IVector<Recording^>^ m_myMusic;

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0


Windows::UI::Xaml::Data [C++]



Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]


Windows::UI::Xaml::Data [C++]





See also

XAML data binding sample
Data binding in depth



© 2016 Microsoft