AbstractConverter.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Globalization;
  3. using System.Windows;
  4. using System.Windows.Data;
  5. using Org.BouncyCastle.X509.Extension;
  6. namespace InABox.WPF;
  7. public class UtilityConverterEventArgs : EventArgs
  8. {
  9. public object Value { get; private set; }
  10. public object Parameter { get; private set; }
  11. public UtilityConverterEventArgs(object value, object parameter)
  12. {
  13. Value = value;
  14. Parameter = parameter;
  15. }
  16. }
  17. public delegate void UtilityCoverterEvent(object sender, UtilityConverterEventArgs args);
  18. public interface IValueConverter<TIn, TOut> : IValueConverter
  19. {
  20. }
  21. // Freezable is a way that might allow us to pass in a DataContext (ie Viewmodel)
  22. // to static resources with XAML. Not yet tested, kept for reference
  23. // The uncertain bit is the DependencyProperty "typeof(IValueConverter)" - this
  24. // might not do what we want, as I suspect it might require a concrete type?
  25. // Ref: https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
  26. public abstract class AbstractConverter<TIn, TOut> : /*Freezable, */ IValueConverter<TIn, TOut>
  27. {
  28. public event UtilityCoverterEvent? Converting;
  29. public event UtilityCoverterEvent? Deconverting;
  30. public abstract TOut Convert(TIn value);
  31. public virtual TIn? Deconvert(TOut value)
  32. {
  33. return default;
  34. }
  35. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  36. {
  37. var typed = value is TIn tin ? tin : default;
  38. Converting?.Invoke(this, new UtilityConverterEventArgs(typed, parameter));
  39. return Convert(typed);
  40. }
  41. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  42. {
  43. var typed = value is TOut tout ? tout : default;
  44. Deconverting?.Invoke(this, new UtilityConverterEventArgs(typed, parameter));
  45. return Deconvert(typed);
  46. }
  47. // #region Freezable
  48. //
  49. // protected override Freezable CreateInstanceCore()
  50. // {
  51. // return (Freezable)Activator.CreateInstance(this.GetType())!;
  52. // }
  53. //
  54. // public static readonly DependencyProperty DataContextProperty =
  55. // DependencyProperty.Register(nameof(DataContext), typeof(object), typeof(IValueConverter),
  56. // new UIPropertyMetadata(null));
  57. //
  58. // public object DataContext
  59. // {
  60. // get { return (object)GetValue(DataContextProperty); }
  61. // set { SetValue(DataContextProperty, value); }
  62. // }
  63. //
  64. // #endregion
  65. }
  66. public class NullConverter<T> : AbstractConverter<T, T>
  67. {
  68. public override T Convert(T value)
  69. {
  70. return value;
  71. }
  72. }