123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using Avalonia;
- using Avalonia.Data.Converters;
- using Shaker.Models;
- using ShakerApp.Tools;
- using ShakerApp.ViewModels;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- namespace ShakerApp.Convert
- {
- public class EnumToBooleanConverter : IValueConverter
- {
- public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- if(value is Enum e1 && parameter is Enum e2 && e1.GetType() == e2.GetType())
- {
- return e1.ToString() == e2.ToString();
- }
- return false;
- }
- public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- public class EnumToDescription : IValueConverter
- {
- public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- if (value == null || value == AvaloniaProperty.UnsetValue || !value.GetType().IsEnum) return "";
- if( value is Enum e) return e.Description();
- return "";
- }
- public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- public class EnumToCollectionConverter : IValueConverter
- {
- public static string Value { get; } = nameof(ValueDescription.Value);
- public static string Key { get; } = nameof(ValueDescription.Key);
- public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- if (value == null || value == AvaloniaProperty.UnsetValue || !value.GetType().IsEnum) return null;
- try
- {
- string parstr = (parameter ==null ?"":parameter.ToString())!;
- List<Enum> showValues = new List<Enum>();
- if (!string.IsNullOrEmpty(parstr)) showValues = parstr.Split(",".ToCharArray()).Select(x => (Enum)Enum.ToObject(value.GetType(), int.Parse(x))).ToList();
- List<ValueDescription> valueDescriptions = new List<ValueDescription>();
- Enum.GetValues(value.GetType())
- .Cast<Enum>()
- .ToList().ForEach(e =>
- {
- if (parameter != null && showValues.Count > 0)
- {
- if (showValues.FindIndex(x => x.ToString() == e.ToString()) >= 0) valueDescriptions.Add(new ValueDescription() { Value = e, Key = e.Description() });
- }
- else valueDescriptions.Add(new ValueDescription() { Value = e, Key = e.Description() });
- });
- return valueDescriptions;
- }
- catch { }
- return null;
- }
- public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- return null;
- }
- }
- internal class ValueDescription : ViewModelBase<IModel>
- {
- private Enum? value;
- private string key = string.Empty;
- private bool visibility = true;
- private bool isEnabled = true;
- public ValueDescription()
- {
- }
- public Enum? Value { get => value; set => SetProperty(ref this.value, value); }
- public string Key { get => key; set => SetProperty(ref key, value); }
- public bool Visibility { get => visibility; set => SetProperty(ref visibility, value); }
- public bool IsEnabled { get => isEnabled; set => SetProperty(ref isEnabled, value); }
- public override string ToString()
- {
- return Key;
- }
- }
- }
|