123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.Globalization;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Data;
- using Org.BouncyCastle.X509.Extension;
- namespace InABox.WPF;
- public class UtilityConverterEventArgs : EventArgs
- {
- public object Value { get; private set; }
- public object Parameter { get; private set; }
- public UtilityConverterEventArgs(object value, object parameter)
- {
- Value = value;
- Parameter = parameter;
- }
- }
- public delegate void UtilityCoverterEvent(object sender, UtilityConverterEventArgs args);
- public interface IValueConverter<TIn, TOut> : IValueConverter
- {
- }
- // Freezable is a way that might allow us to pass in a DataContext (ie Viewmodel)
- // to static resources with XAML. Not yet tested, kept for reference
- // The uncertain bit is the DependencyProperty "typeof(IValueConverter)" - this
- // might not do what we want, as I suspect it might require a concrete type?
- // Ref: https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
- public abstract class AbstractConverter<TIn, TOut> : /*Freezable, */ IValueConverter<TIn, TOut>
- {
- public event UtilityCoverterEvent? Converting;
- public event UtilityCoverterEvent? Deconverting;
- public abstract TOut Convert(TIn value);
- public virtual TIn? Deconvert(TOut value)
- {
- return default;
- }
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- var typed = value is TIn tin ? tin : default;
- Converting?.Invoke(this, new UtilityConverterEventArgs(typed, parameter));
- return Convert(typed);
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- var typed = value is TOut tout ? tout : default;
- Deconverting?.Invoke(this, new UtilityConverterEventArgs(typed, parameter));
- return Deconvert(typed);
- }
- // #region Freezable
- //
- // protected override Freezable CreateInstanceCore()
- // {
- // return (Freezable)Activator.CreateInstance(this.GetType())!;
- // }
- //
- // public static readonly DependencyProperty DataContextProperty =
- // DependencyProperty.Register(nameof(DataContext), typeof(object), typeof(IValueConverter),
- // new UIPropertyMetadata(null));
- //
- // public object DataContext
- // {
- // get { return (object)GetValue(DataContextProperty); }
- // set { SetValue(DataContextProperty, value); }
- // }
- //
- // #endregion
- }
|