private object ConvertbackValue(object value, Type targetType)
{
if (value != null && value.GetType() == targetType)
return value;
object convertedValue = value;
var memberType = Member.GetMemberType();
try
{
if (ValueConverter != null)
{
var parameter = GetConverterParameter();
convertedValue = ValueConverter.ConvertBack(value, memberType, parameter, CultureInfo.CurrentUICulture);
}
if (targetType != null)
{
var typeCode = System.Convert.GetTypeCode(convertedValue);
if (typeCode != TypeCode.Object && typeCode != TypeCode.Empty)
{
try
{
convertedValue = System.Convert.ChangeType(convertedValue, targetType);
}
catch (InvalidCastException)
{
}
}
}
}
catch (FormatException)
{
var message = string.Format("The value \"{0}\" is of type {1} but the {2} \"{3}\" is of type {4}. You need to specify an IValueConverter to convert it.",
convertedValue, convertedValue.GetType(), Member.GetMemberTypeName(), Member.Name, memberType);
throw new FormatException(message);
}
catch (NotImplementedException)
{
}
return convertedValue;
}