public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (compiledExpression == null) { if ((compiledExpression = CompileExpression(null, (string)parameter, true, new List<Type>{targetType})) == null) return null; } if (compiledInversedExpression == null) { //try convert back expression try { var resType = compiledExpression.Expression.Type; var param = Expression.Parameter(resType, "Path"); compiledInversedExpression = new Inverter(parser).InverseExpression(compiledExpression.Expression, param); } catch (Exception e) { Trace.WriteLine("Binding error: calc converter can't convert back expression " + parameter + ": " + e.Message); } } if (compiledInversedExpression != null) { try { if (targetType == typeof(bool) && value.GetType() == typeof(Visibility)) value = new BoolToVisibilityConverter(FalseToVisibility) .ConvertBack(value, targetType, null, culture); if (value is string && compiledExpression.Expression.Type != value.GetType()) value = ParseStringToObject((string)value, compiledExpression.Expression.Type); var source = compiledInversedExpression.Invoke(value); return source; } catch (Exception e) { Trace.WriteLine("Binding error: calc converter can't invoke back expression " + parameter + ": " + e.Message); } } return null; }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values == null) return null; if (sourceValuesTypes == null) { sourceValuesTypes = GetTypes(values); } else { var currentValuesTypes = GetTypes(values); if (!sourceValuesTypes.SequenceEqual(currentValuesTypes)) { sourceValuesTypes = currentValuesTypes; compiledExpression = null; compiledInversedExpression = null; } } if (compiledExpression == null) { if ((compiledExpression = CompileExpression(values, (string)parameter)) == null) return null; } try { var result = compiledExpression.Invoke(values); if (!StringFormatDefined) { if (targetType == typeof(Visibility)) { result = new BoolToVisibilityConverter(FalseToVisibility) .Convert(result, targetType, null, culture); } if (targetType == typeof(String)) result = String.Format(CultureInfo.InvariantCulture, "{0}", result); } return result; } catch (Exception e) { Trace.WriteLine("Binding error: calc converter can't invoke expression " + compiledExpression.ExpressionText + ": " + e.Message); return null; } }