Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

IValueConverter.ConvertBack Method

Converts a value. The data binding engine calls this method when it propagates a value from the binding target to the binding source.

Namespace: System.Windows.Data
Assembly: PresentationFramework (in presentationframework.dll)

Object ConvertBack (
	Object value,
	Type targetType,
	Object parameter,
	CultureInfo culture
)
Object ConvertBack (
	Object value, 
	Type targetType, 
	Object parameter, 
	CultureInfo culture
)
function ConvertBack (
	value : Object, 
	targetType : Type, 
	parameter : Object, 
	culture : CultureInfo
) : Object
You cannot use methods in XAML.

Parameters

value

The value that is produced by the binding target.

targetType

The type to convert to.

parameter

The converter parameter to use.

culture

The culture to use in the converter.

Return Value

A converted value. If the method returns a null reference (Nothing in Visual Basic), the valid a null reference (Nothing in Visual Basic) value is used. A return value of DependencyProperty.UnsetValue indicates that the converter produced no value and that to the binding uses the FallbackValue, if available, or the default value instead. A return value of Binding.DoNothing indicates that the binding does not transfer the value or use the FallbackValue or default value.

The implementation of this method needs to be the inverse of the Convert method.

The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the ConvertBack method, or any uncaught exceptions that are thrown by methods that the ConvertBack method calls, are treated as run-time errors. Handle anticipated problems by returning DependencyProperty.UnsetValue.

This example shows how to apply conversion to data that is used in bindings.

To convert data during binding, you must create a class that implements the IValueConverter interface, which includes the Convert and ConvertBack methods.

The following example shows the implementation of a date converter that converts the date value passed in so that it only shows the year, the month, and the day. When implementing the IValueConverter interface, it is a good practice to decorate the implementation with a ValueConversionAttribute attribute to indicate to development tools the data types involved in the conversion, as in the following example:

[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime date = (DateTime)value;
        return date.ToShortDateString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = value.ToString();
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return value;
    }
}

Once you have created a converter, you can add it as a resource in your Extensible Application Markup Language (XAML) file. In the following example, src maps to the namespace in which DateConverter is defined.

<src:DateConverter x:Key="dateConverter"/>

Finally, you can use the converter in your binding using the following syntax. In the following example, the text content of the TextBlock is bound to StartDate, which is a property of an external data source.

<TextBlock Grid.Row="2" Grid.Column="0" Margin="0,0,8,0"
           Name="startDateTitle"
           Style="{StaticResource smallTitleStyle}">Start Date:</TextBlock>
<TextBlock Name="StartDateDTKey" Grid.Row="2" Grid.Column="1" 
    Text="{Binding Path=StartDate, Converter={StaticResource dateConverter}}" 
    Style="{StaticResource textStyleTextBlock}"/>

The style resources referenced in the above example are defined in the resource section of the sample not shown in this topic. For the complete code sample from which the above code examples were extracted, see Data Binding Demo.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft