Browse Source

Added an optional "ConvertBack" method to FuncValueConverter (#19920)

pull/20055/head
zacfromaustinpowder 11 months ago
committed by GitHub
parent
commit
5ab8604436
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 56
      src/Avalonia.Base/Data/Converters/FuncValueConverter.cs

56
src/Avalonia.Base/Data/Converters/FuncValueConverter.cs

@ -13,16 +13,28 @@ namespace Avalonia.Data.Converters
public class FuncValueConverter<TIn, TOut> : IValueConverter public class FuncValueConverter<TIn, TOut> : IValueConverter
{ {
private readonly Func<TIn?, TOut> _convert; private readonly Func<TIn?, TOut> _convert;
private readonly Func<TOut?, TIn>? _convertBack;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FuncValueConverter{TIn, TOut}"/> class. /// Initializes a new instance of the <see cref="FuncValueConverter{TIn, TOut}"/> class.
/// </summary> /// </summary>
/// <param name="convert">The convert function.</param> /// <param name="convert">The function to convert TIn to TOut.</param>
public FuncValueConverter(Func<TIn?, TOut> convert) public FuncValueConverter(Func<TIn?, TOut> convert)
{ {
_convert = convert; _convert = convert;
} }
/// <summary>
/// Initializes a new instance of the <see cref="FuncValueConverter{TIn, TOut}"/> class.
/// </summary>
/// <param name="convert">The function to convert TIn to TOut.</param>
/// <param name="convertBack">The function to convert TOut back to In.</param>
public FuncValueConverter(Func<TIn?, TOut> convert, Func<TOut?, TIn>? convertBack)
{
_convert = convert;
_convertBack = convertBack;
}
/// <inheritdoc/> /// <inheritdoc/>
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
@ -39,7 +51,19 @@ namespace Avalonia.Data.Converters
/// <inheritdoc/> /// <inheritdoc/>
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
throw new NotImplementedException(); if (_convertBack == null)
{
throw new NotImplementedException();
}
if (TypeUtilities.CanCast<TOut>(value))
{
return _convertBack((TOut?)value);
}
else
{
return AvaloniaProperty.UnsetValue;
}
} }
} }
@ -53,16 +77,28 @@ namespace Avalonia.Data.Converters
public class FuncValueConverter<TIn, TParam, TOut> : IValueConverter public class FuncValueConverter<TIn, TParam, TOut> : IValueConverter
{ {
private readonly Func<TIn?, TParam?, TOut> _convert; private readonly Func<TIn?, TParam?, TOut> _convert;
private readonly Func<TOut?, TParam?, TIn>? _convertBack;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FuncValueConverter{TIn, TParam, TOut}"/> class. /// Initializes a new instance of the <see cref="FuncValueConverter{TIn, TParam, TOut}"/> class.
/// </summary> /// </summary>
/// <param name="convert">The convert function.</param> /// <param name="convert">The function to convert TIn to TOut.</param>
public FuncValueConverter(Func<TIn?, TParam?, TOut> convert) public FuncValueConverter(Func<TIn?, TParam?, TOut> convert)
{ {
_convert = convert; _convert = convert;
} }
/// <summary>
/// Initializes a new instance of the <see cref="FuncValueConverter{TIn, TParam, TOut}"/> class.
/// </summary>
/// <param name="convert">The function to convert TIn to TOut.</param>
/// <param name="convertBack">The function to convert TOut back to In.</param>
public FuncValueConverter(Func<TIn?, TParam?, TOut> convert, Func<TOut?, TParam?, TIn>? convertBack = null)
{
_convert = convert;
_convertBack = convertBack;
}
/// <inheritdoc/> /// <inheritdoc/>
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
@ -79,7 +115,19 @@ namespace Avalonia.Data.Converters
/// <inheritdoc/> /// <inheritdoc/>
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{ {
throw new NotImplementedException(); if (_convertBack == null)
{
throw new NotImplementedException();
}
if (TypeUtilities.CanCast<TOut>(value) && TypeUtilities.CanCast<TParam>(parameter))
{
return _convertBack((TOut?)value, (TParam?)parameter);
}
else
{
return AvaloniaProperty.UnsetValue;
}
} }
} }
} }

Loading…
Cancel
Save