StringSubstringConverter.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Data;
  4. namespace Muchinfo.MTPClient.Infrastructure.Converters
  5. {
  6. //datagrid显示太长(显示部分数据后面跟省略号)
  7. public class StringSubstringConverter : IValueConverter
  8. {
  9. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  10. {
  11. string result = string.Empty;
  12. if (value != null)
  13. {
  14. string srt = (string)value;
  15. if (!string.IsNullOrEmpty(srt) && srt.Length > 20)
  16. {
  17. result = srt.Substring(0, 17) + "...";
  18. }
  19. else
  20. {
  21. return value;
  22. }
  23. }
  24. return result;
  25. }
  26. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. }
  31. //datagrid根据文本长度是否显示tooltip
  32. public class StringVisableConver : IValueConverter
  33. {
  34. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  35. {
  36. Visibility result = Visibility.Visible;
  37. string srt = (string)value;
  38. if (string.IsNullOrWhiteSpace(srt))
  39. {
  40. result = Visibility.Collapsed;
  41. }
  42. else
  43. {
  44. if (srt.Length <= 20)
  45. {
  46. result = Visibility.Collapsed;
  47. }
  48. }
  49. return result;
  50. }
  51. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  52. {
  53. throw new NotImplementedException();
  54. }
  55. }
  56. }