EnumToDescription.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. else if(value is Enum e && parameter is string s)
  21. {
  22. return e.ToString() == s;
  23. }
  24. return false;
  25. }
  26. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. }
  31. public class EnumToDescription : IValueConverter
  32. {
  33. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  34. {
  35. if (value == null || value == AvaloniaProperty.UnsetValue || !value.GetType().IsEnum) return "";
  36. if( value is Enum e) return e.Description();
  37. return "";
  38. }
  39. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  40. {
  41. throw new NotImplementedException();
  42. }
  43. }
  44. public class EnumToCollectionConverter : IValueConverter
  45. {
  46. public static string Value { get; } = nameof(ValueDescription.Value);
  47. public static string Key { get; } = nameof(ValueDescription.Key);
  48. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  49. {
  50. if (value == null || value == AvaloniaProperty.UnsetValue || !value.GetType().IsEnum) return null;
  51. try
  52. {
  53. string parstr = (parameter ==null ?"":parameter.ToString())!;
  54. List<Enum> showValues = new List<Enum>();
  55. if (!string.IsNullOrEmpty(parstr)) showValues = parstr.Split(",".ToCharArray()).Select(x => (Enum)Enum.ToObject(value.GetType(), int.Parse(x))).ToList();
  56. List<ValueDescription> valueDescriptions = new List<ValueDescription>();
  57. Enum.GetValues(value.GetType())
  58. .Cast<Enum>()
  59. .ToList().ForEach(e =>
  60. {
  61. if (parameter != null && showValues.Count > 0)
  62. {
  63. if (showValues.FindIndex(x => x.ToString() == e.ToString()) >= 0) valueDescriptions.Add(new ValueDescription() { Value = e, Key = e.Description() });
  64. }
  65. else valueDescriptions.Add(new ValueDescription() { Value = e, Key = e.Description() });
  66. });
  67. return valueDescriptions;
  68. }
  69. catch { }
  70. return null;
  71. }
  72. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  73. {
  74. return null;
  75. }
  76. }
  77. internal class ValueDescription : DisplayViewModelBase<IModel>
  78. {
  79. private Enum? value;
  80. private string key = string.Empty;
  81. private bool visibility = true;
  82. private bool isEnabled = true;
  83. public ValueDescription()
  84. {
  85. }
  86. public Enum? Value { get => value; set => SetProperty(ref this.value, value); }
  87. public string Key { get => key; set => SetProperty(ref key, value); }
  88. public bool Visibility { get => visibility; set => SetProperty(ref visibility, value); }
  89. public bool IsEnabled { get => isEnabled; set => SetProperty(ref isEnabled, value); }
  90. public override string ToString()
  91. {
  92. return Key;
  93. }
  94. }
  95. }