EnumToDescription.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Avalonia;
  2. using Avalonia.Data.Converters;
  3. using IViewModel.Tools;
  4. using IViewModel.ViewModels;
  5. using Shaker.Models;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.Linq;
  10. namespace IViewModel.Convert
  11. {
  12. public sealed class EnumToBooleanConverter : IValueConverter
  13. {
  14. public static EnumToBooleanConverter Instance { get; } = new EnumToBooleanConverter();
  15. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  16. {
  17. if(value is Enum e1 &&parameter is Enum e2 && e1.GetType() == e2.GetType())
  18. {
  19. return e1.ToString() == e2.ToString();
  20. }
  21. else if(value is Enum e && parameter is string s)
  22. {
  23. return e.ToString() == s;
  24. }
  25. return false;
  26. }
  27. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  28. {
  29. throw new NotImplementedException();
  30. }
  31. }
  32. public class EnumToDescription : IValueConverter
  33. {
  34. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  35. {
  36. if (value == null || value == AvaloniaProperty.UnsetValue || !value.GetType().IsEnum) return "";
  37. if( value is Enum e) return e.Description();
  38. return "";
  39. }
  40. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  41. {
  42. throw new NotImplementedException();
  43. }
  44. }
  45. public sealed class EnumToCollectionConverter : IValueConverter
  46. {
  47. public static EnumToCollectionConverter Instance { get; } = new EnumToCollectionConverter();
  48. public static string Value { get; } = nameof(ValueDescription.Value);
  49. public static string Key { get; } = nameof(ValueDescription.Key);
  50. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  51. {
  52. if (value == null || value == AvaloniaProperty.UnsetValue || !value.GetType().IsEnum) return null;
  53. try
  54. {
  55. string parstr = (parameter ==null ?"":parameter.ToString())!;
  56. List<Enum> showValues = new List<Enum>();
  57. if (!string.IsNullOrEmpty(parstr)) showValues = parstr.Split(",".ToCharArray()).Select(x => (Enum)Enum.ToObject(value.GetType(), int.Parse(x))).ToList();
  58. List<ValueDescription> valueDescriptions = new List<ValueDescription>();
  59. Enum.GetValues(value.GetType())
  60. .Cast<Enum>()
  61. .ToList().ForEach(e =>
  62. {
  63. if (parameter != null && showValues.Count > 0)
  64. {
  65. if (showValues.FindIndex(x => x.ToString() == e.ToString()) >= 0) valueDescriptions.Add(new ValueDescription() { Value = e, Key = e.Description() });
  66. }
  67. else valueDescriptions.Add(new ValueDescription() { Value = e, Key = e.Description() });
  68. });
  69. return valueDescriptions;
  70. }
  71. catch { }
  72. return null;
  73. }
  74. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  75. {
  76. return null;
  77. }
  78. }
  79. public sealed class ValueDescription : ViewModelBase
  80. {
  81. private Enum? value;
  82. private string key = string.Empty;
  83. private bool visibility = true;
  84. private bool isEnabled = true;
  85. public ValueDescription()
  86. {
  87. }
  88. public Enum? Value { get => value; set => SetProperty(ref this.value, value); }
  89. public string Key { get => key; set => SetProperty(ref key, value); }
  90. public bool Visibility { get => visibility; set => SetProperty(ref visibility, value); }
  91. public bool IsEnabled { get => isEnabled; set => SetProperty(ref isEnabled, value); }
  92. public override string ToString()
  93. {
  94. return Key;
  95. }
  96. }
  97. }