Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class StringFormatConverter : IMultiValueConverter
- {
- Dictionary<int, Tuple<int, object, IMultiValueConverter>> nestedConverters = new Dictionary<int, Tuple<int, object, IMultiValueConverter>>();
- public void AddNestedConverter(IMultiValueConverter c, object parameter, int fromidx, int count)
- {
- nestedConverters.Add(fromidx, Tuple.Create(count, parameter, c));
- }
- List<object> Simplify(object[] values, CultureInfo culture)
- {
- List<object> result = new List<object>();
- for (int i = 0; i < values.Length; /**/)
- {
- object curr;
- Tuple<int, object, IMultiValueConverter> nested;
- if (nestedConverters.TryGetValue(i, out nested))
- {
- var nestedConverter = nested.Item3;
- var parameter = nested.Item2;
- var length = nested.Item1;
- var nestedValues = new object[length];
- Array.Copy(values, i, nestedValues, 0, length);
- curr = nestedConverter.Convert(nestedValues, typeof(object), parameter, culture);
- i += length;
- }
- else
- {
- curr = values[i];
- i++;
- }
- result.Add(curr);
- }
- return result;
- }
- public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
- {
- var simplifiedValues = Simplify(values, culture);
- var format = (string)simplifiedValues[0];
- return string.Format(format, simplifiedValues.Skip(1).ToArray());
- }
- public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement