Browse Source
Added a constructor to FuncMultiValueConverter that takes an IReadOnlyList instead of an IEnumerable (#19936)
Co-authored-by: Max Katz <maxkatz6@outlook.com>
pull/20055/head
zacfromaustinpowder
3 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
26 additions and
3 deletions
-
src/Avalonia.Base/Data/Converters/FuncMultiValueConverter.cs
-
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_MultiBinding.cs
|
|
|
@ -13,17 +13,26 @@ namespace Avalonia.Data.Converters |
|
|
|
/// <typeparam name="TOut">The output type.</typeparam>
|
|
|
|
public class FuncMultiValueConverter<TIn, TOut> : IMultiValueConverter |
|
|
|
{ |
|
|
|
private readonly Func<IEnumerable<TIn?>, TOut> _convert; |
|
|
|
private readonly Func<IReadOnlyList<TIn?>, TOut> _convert; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="FuncValueConverter{TIn, TOut}"/> class.
|
|
|
|
/// Initializes a new instance of the <see cref="FuncMultiValueConverter{TIn, TOut}"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="convert">The convert function.</param>
|
|
|
|
public FuncMultiValueConverter(Func<IEnumerable<TIn?>, TOut> convert) |
|
|
|
public FuncMultiValueConverter(Func<IReadOnlyList<TIn?>, TOut> convert) |
|
|
|
{ |
|
|
|
_convert = convert; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="FuncMultiValueConverter{TIn, TOut}"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="convert">The convert function.</param>
|
|
|
|
public FuncMultiValueConverter(Func<IEnumerable<TIn?>, TOut> convert) |
|
|
|
: this(new Func<IReadOnlyList<TIn?>, TOut>(convert)) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture) |
|
|
|
{ |
|
|
|
|
|
|
|
@ -153,6 +153,20 @@ namespace Avalonia.Base.UnitTests |
|
|
|
|
|
|
|
Assert.Equal(",Bar,Baz", value); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void MultiValueConverter_Supports_Indexing_The_Parameters() |
|
|
|
{ |
|
|
|
var target = new FuncMultiValueConverter<string, string>(v => v[0]); |
|
|
|
|
|
|
|
object value = target.Convert(new[] { "Foo", "Bar", "Baz" }, typeof(string), null, CultureInfo.InvariantCulture); |
|
|
|
|
|
|
|
Assert.Equal("Foo", value); |
|
|
|
|
|
|
|
value = target.Convert(new[] { null, "Bar", "Baz" }, typeof(string), null, CultureInfo.InvariantCulture); |
|
|
|
|
|
|
|
Assert.Null(value); |
|
|
|
} |
|
|
|
|
|
|
|
private struct StringValueTypeWrapper |
|
|
|
{ |
|
|
|
|