Converters.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Globalization;
  3. using System.Windows;
  4. using System.Windows.Data;
  5. using System.Windows.Markup;
  6. namespace AIPractice.LabellerServer.WPF
  7. {
  8. [MarkupExtensionReturnType(typeof(IValueConverter))]
  9. public class BoolToVisibleConverter : MarkupExtension, IValueConverter
  10. {
  11. public static BoolToVisibleConverter Converter { get; private set; }
  12. static BoolToVisibleConverter()
  13. {
  14. Converter = new BoolToVisibleConverter();
  15. }
  16. public override object ProvideValue(IServiceProvider serviceProvider)
  17. {
  18. return Converter ?? (Converter = new BoolToVisibleConverter());
  19. }
  20. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  21. {
  22. if (value != null)
  23. {
  24. var b = (bool)value;
  25. return b ? Visibility.Visible : Visibility.Collapsed;
  26. }
  27. return Visibility.Collapsed;
  28. }
  29. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  30. {
  31. throw new NotImplementedException();
  32. }
  33. }
  34. [MarkupExtensionReturnType(typeof(IValueConverter))]
  35. public class LabelNameConverter : MarkupExtension, IValueConverter
  36. {
  37. public static LabelNameConverter Converter { get; private set; }
  38. static LabelNameConverter()
  39. {
  40. Converter = new LabelNameConverter();
  41. }
  42. public override object ProvideValue(IServiceProvider serviceProvider)
  43. {
  44. return Converter ?? (Converter = new LabelNameConverter());
  45. }
  46. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  47. {
  48. if (value != null)
  49. {
  50. var name = (string)value;
  51. return string.IsNullOrEmpty(name) ? "默认标签" : name;
  52. }
  53. return "默认标签";
  54. }
  55. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  56. {
  57. throw new NotImplementedException();
  58. }
  59. }
  60. }