Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
Other versions are also available for the following:
.NET Framework Class Library for Silverlight
IValueConverter Interface
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Exposes methods that allow modifying the data as it passes through the binding engine.

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

Visual Basic (Declaration)
Public Interface IValueConverter
Visual Basic (Usage)
Dim instance As IValueConverter
C#
public interface IValueConverter
Visual C++
public interface class IValueConverter
JScript
public interface IValueConverter

The following example shows how to implement the IValueConverter interface.

C#
//Custom class implements the IValueConverter interface
public class DateToStringConverter : IValueConverter
{

    #region IValueConverter Members

    //Define the Convert method to change DateTime object to month string
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //value is the data from the source object
        DateTime thisdate = (DateTime)value;
        int monthnum = thisdate.Month;
        string month;
        switch (monthnum)
        {
            case 1:
                month = "January";
                break;
            case 2:
                month = "February";
                break;
            default:
                month = "Month not found";
                break;
        }
        //return the value to pass to the target
        return month;

    }

    //ConvertBack not implemented for OneWay binding
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker