ListToStringConverter.cs 765 B

123456789101112131415161718192021222324252627282930313233
  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. return ss.ToList();
  29. }
  30. }
  31. }