EnumToDescription.cs 3.8 KB

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