The following example shows how to implement the IValueConverter interface.
|
//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
}
|