ListToStringConverter.cs 844 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Windows.Data;
  6. namespace Tree
  7. {
  8. [ValueConversion(typeof(List<string>), typeof(string))]
  9. public class ListToStringConverter : IValueConverter
  10. {
  11. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  12. {
  13. if (value == null)
  14. {
  15. return "";
  16. }
  17. var list = (List<string>) value;
  18. return String.Join(",", list);
  19. }
  20. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  21. {
  22. if (value == null)
  23. {
  24. return new List<string>();
  25. }
  26. var s = (string) value;
  27. string[] ss = s.Split(',');
  28. for (int i = 0; i < ss.Length; ++i)
  29. {
  30. ss[i] = ss[i].Trim();
  31. }
  32. return ss.ToList();
  33. }
  34. }
  35. }