public void ConvertTest()
        {
            var converter = new NumberToPercentConverter();
            object value = null;
            object parameters = null;

            var result = converter.Convert(value, typeof(object), parameters, CultureInfo.InvariantCulture);
            Assert.IsNull(result);

            // if not decimal return value
            const int IntValue = 100;
            value = IntValue;
            result = converter.Convert(value, typeof(object), parameters, CultureInfo.InvariantCulture);
            Assert.AreEqual(result, IntValue);

            value = 100m;
            result = converter.Convert(value, typeof(object), parameters, CultureInfo.InvariantCulture);
            Assert.AreEqual(result, 1m);

            value = 0m;
            result = converter.Convert(value, typeof(object), parameters, CultureInfo.InvariantCulture);
            Assert.AreEqual(result, 0m);

            value = 233m;
            result = converter.Convert(value, typeof(object), parameters, CultureInfo.InvariantCulture);
            Assert.AreEqual(result, 2.33m);
        }
示例#2
0
        private static ConverterInfo GetConverterInfo(IColumnItem column)
        {
            var localProperty = column.Property;
            IValueConverter converterObject = null;
            object converterParameter = null;
            var converterString = string.Empty;
            PropertyInfo sourceProperty = null;

            if (ReferenceFieldsHelper.IsReferenceProperty(localProperty))
            {
                // Field type specific attributes should be taken from source.
                var refInfo = ReferenceFieldsHelper.GetCrossRefInfo(localProperty);
                sourceProperty = new ReferenceFieldsHelper().GetSourcePropertyInfo(refInfo.Item1, refInfo.Item2);
            }

            var prop = sourceProperty ?? localProperty;

            if (PropertyIsDateTime(localProperty, column))
            {
                var datetimeFormat = ReferenceFieldsHelper.GetDateTimeFormat(prop);

                if (!string.IsNullOrEmpty(datetimeFormat))
                {
                    converterObject = new DateTimeFormatConverter();
                    converterString = string.Format(CultureInfo.InvariantCulture, "Converter={{StaticResource DateTimeFormatConverter}}, ConverterParameter={0}", datetimeFormat);
                }
            }
            else if (localProperty.PropertyType == typeof(string))
            {
                var approvalAttribute = (ApprovalDefinitionAttribute)(sourceProperty ?? prop).GetCustomAttributes(typeof(ApprovalDefinitionAttribute), false).FirstOrDefault();

                if (approvalAttribute != null)
                {
                    converterObject = new ApprovalStateToDisplayNameConverter();
                    converterString = string.Format(CultureInfo.InvariantCulture, "Converter={{StaticResource ApprovalStateToDisplayNameConverter}}");
                }
            }
            else if (prop.PropertyType == typeof(bool?))
            {
                var checkboxSettings = ReferenceFieldsHelper.GetCheckboxSettings(prop);

                if (checkboxSettings != null && checkboxSettings.IsSwitchToggle)
                {
                    var param = new[]
                                            {
                                                checkboxSettings.UndefinedLabel,
                                                checkboxSettings.FalseLabel,
                                                checkboxSettings.TrueLabel
                                            };

                    converterObject = new CheckboxToCustomValueConverter();
                    converterParameter = string.Join("~", param).Replace(' ', '_').Replace('\'', '^');
                    converterString = string.Format(CultureInfo.InvariantCulture, "Converter={{StaticResource checkboxToCustomValueConverter}}, ConverterParameter={0}", converterParameter);
                }
            }

            var numericAttribute = (NumericAttribute)prop.GetCustomAttributes(typeof(NumericAttribute), false).FirstOrDefault();

            if (numericAttribute != null)
            {
                switch (numericAttribute.NumericType)
                {
                    case NumericTypes.Percentage:
                        converterObject = new NumberToPercentConverter();
                        converterString = string.Format(CultureInfo.InvariantCulture, "Converter={{StaticResource NumberToPercentConverter}}");
                        break;
                }
            }

            return new ConverterInfo(converterObject, converterParameter, converterString);
        }