using System.Windows.Data; namespace Aero.Converters.Patterns { public interface ICompositeConverter : IValueConverter { IValueConverter PostConverter { get; set; } object PostConverterParameter { get; set; } } }
using System; using System.Globalization; using System.Windows.Data; namespace Aero.Converters.Patterns { public class ConverterEventArgs : EventArgs { public object ConvertedValue { get; set; } public object Value { get; private set; } public Type TargetType { get; private set; } public object Parameter { get; private set; } public CultureInfo Culture { get; private set; } public ConverterEventArgs(object value, Type targetType, object parameter, CultureInfo culture) { TargetType = targetType; Parameter = parameter; Culture = culture; Value = value; } } public interface IInlineConverter : IValueConverter { event EventHandler<ConverterEventArgs> Converting; event EventHandler<ConverterEventArgs> ConvertingBack; } //public interface IInlineConverter : IValueConverter //{ // event Func<object, Type, object, CultureInfo, object> Converting; // event Func<object, Type, object, CultureInfo, object> ConvertingBack; //} }
using System; using System.Globalization; using System.Windows.Data; using Aero.Converters.Patterns; namespace Aero.Converters { public class InlineConverter : IInlineConverter, ICompositeConverter { public IValueConverter PostConverter { get; set; } public object PostConverterParameter { get; set; } public event EventHandler<ConverterEventArgs> Converting; public event EventHandler<ConverterEventArgs> ConvertingBack; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var args = new ConverterEventArgs(value, targetType, parameter, culture); var handler = Converting; if (handler != null) handler(this, args); return PostConverter == null ? args.ConvertedValue : PostConverter.Convert(args.ConvertedValue, targetType, PostConverterParameter, culture); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { var args = new ConverterEventArgs(value, targetType, parameter, culture); var handler = ConvertingBack; if (handler != null) handler(this, args); return PostConverter == null ? args.ConvertedValue : PostConverter.ConvertBack(args.ConvertedValue, targetType, PostConverterParameter, culture); } } }
<Grid> <Grid.Resources> <InlineConverter x:Key="InlineConverter" Converting="InlineConverter_OnConverting"/> </Grid.Resources> <TextBlock Text="{Binding Number, Converter={StaticResource InlineConverter}}"/> </Grid>
private void InlineConverter_OnConverting(object sender, ConverterEventArgs e) { e.ConvertedValue = string.Format("Title: {0} \nDataContext:\n{1} \nConverter Value: {2}", Title, DataContext, e.Value); }
Source: https://habr.com/ru/post/276185/
All Articles