Ejemplo n.º 1
0
        /// <inheritdoc/>
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // We get the length first to always throw an exception if the parameter in incorrect
            int length;

            try
            {
                length = ConverterHelper.ConvertToInt32(parameter, culture);
                if (length == 0)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                throw new FormatException("The parameter must be convertible to a non-null integer.");
            }

            if (value == null)
            {
                return(null);
            }

            var addEllipsis = length >= 0;

            length = Math.Abs(length);
            var str = value.ToString();

            return(str.Length > length?str.Substring(0, length) + (addEllipsis ? "..." : "") : str);
        }
Ejemplo n.º 2
0
 /// <inheritdoc/>
 public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
 {
     try
     {
         var charValue = ConverterHelper.ConvertToChar(ConverterHelper.ConvertToInt32(value, culture), culture);
         return(charValue);
     }
     catch (Exception)
     {
         return(default(char));
     }
 }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var collection = (IList)parameter;

            if (collection == null)
            {
                return(null);
            }

            var index = ConverterHelper.ConvertToInt32(value ?? -1, culture);

            if (index < 0 || index >= collection.Count)
            {
                return(null);
            }

            return(collection[index]);
        }
Ejemplo n.º 4
0
        /// <inheritdoc/>
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var unicodeValue = ConverterHelper.ConvertToInt32(value, culture);

            return(unicodeValue);
        }