Example #1
0
        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(_fallbackValue);
                }
            }

            try
            {
                var result = _compiledExpression.Invoke(values);

                if (!StringFormatDefined)
                {
                    if (targetType == typeof(Visibility))
                    {
                        if (!(result is 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)
            {
                Tracer.TraceError("Can't invoke expression " + _compiledExpression.ExpressionText + ": " + e.Message);
                return(null);
            }
        }
Example #2
0
        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)
                {
                    Tracer.TraceError("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)
                {
                    Tracer.TraceError("Can't invoke back expression " + parameter + ": " + e.Message);
                }
            }
            return(null);
        }
Example #3
0
        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;
        }
Example #4
0
        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;
            }
        }