Advertisement
Guest User

Untitled

a guest
Aug 24th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. class StringFormatConverter : IMultiValueConverter
  2. {
  3.     Dictionary<int, Tuple<int, object, IMultiValueConverter>> nestedConverters = new Dictionary<int, Tuple<int, object, IMultiValueConverter>>();
  4.     public void AddNestedConverter(IMultiValueConverter c, object parameter, int fromidx, int count)
  5.     {
  6.         nestedConverters.Add(fromidx, Tuple.Create(count, parameter, c));
  7.     }
  8.  
  9.     List<object> Simplify(object[] values, CultureInfo culture)
  10.     {
  11.         List<object> result = new List<object>();
  12.  
  13.         for (int i = 0; i < values.Length; /**/)
  14.         {
  15.             object curr;
  16.             Tuple<int, object, IMultiValueConverter> nested;
  17.             if (nestedConverters.TryGetValue(i, out nested))
  18.             {
  19.                 var nestedConverter = nested.Item3;
  20.                 var parameter = nested.Item2;
  21.                 var length = nested.Item1;
  22.                 var nestedValues = new object[length];
  23.                 Array.Copy(values, i, nestedValues, 0, length);
  24.                 curr = nestedConverter.Convert(nestedValues, typeof(object), parameter, culture);
  25.                 i += length;
  26.             }
  27.             else
  28.             {
  29.                 curr = values[i];
  30.                 i++;
  31.             }
  32.             result.Add(curr);
  33.         }
  34.  
  35.         return result;
  36.     }
  37.  
  38.     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  39.     {
  40.         var simplifiedValues = Simplify(values, culture);
  41.         var format = (string)simplifiedValues[0];
  42.         return string.Format(format, simplifiedValues.Skip(1).ToArray());
  43.     }
  44.  
  45.     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  46.     {
  47.         throw new NotImplementedException();
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement