Browse Source

Merge pull request #5007 from workgroupengineering/fixes/Issue_5006

Fixes Issue 5006
pull/5013/head
Dan Walmsley 5 years ago
committed by GitHub
parent
commit
6b71270280
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/Markup/Avalonia.Markup/Data/MultiBinding.cs
  2. 23
      tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests.cs
  3. 21
      tests/Avalonia.Markup.UnitTests/Extensions/IEnummerableExtension.cs

17
src/Markup/Avalonia.Markup/Data/MultiBinding.cs

@ -48,7 +48,7 @@ namespace Avalonia.Data
/// Gets or sets the binding priority.
/// </summary>
public BindingPriority Priority { get; set; }
/// <summary>
/// Gets or sets the relative source for the binding.
/// </summary>
@ -77,12 +77,12 @@ namespace Avalonia.Data
// We only respect `StringFormat` if the type of the property we're assigning to will
// accept a string. Note that this is slightly different to WPF in that WPF only applies
// `StringFormat` for target type `string` (not `object`).
if (!string.IsNullOrWhiteSpace(StringFormat) &&
if (!string.IsNullOrWhiteSpace(StringFormat) &&
(targetType == typeof(string) || targetType == typeof(object)))
{
converter = new StringFormatMultiValueConverter(StringFormat, converter);
}
var children = Bindings.Select(x => x.Initiate(target, null));
var input = children.Select(x => x.Observable)
@ -116,7 +116,16 @@ namespace Avalonia.Data
}
var culture = CultureInfo.CurrentCulture;
var converted = converter.Convert(values, targetType, ConverterParameter, culture);
values = new System.Collections.ObjectModel.ReadOnlyCollection<object>(values);
object converted;
if (converter != null)
{
converted = converter.Convert(values, targetType, ConverterParameter, culture);
}
else
{
converted = values;
}
if (converted == null)
{

23
tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests.cs

@ -157,6 +157,29 @@ namespace Avalonia.Markup.UnitTests.Data
Assert.Equal("1,2,Fallback", target.Text);
}
[Fact]
public void MultiBinding_Without_StringFormat_And_Converter()
{
var source = new { A = 1, B = 2, C = 3 };
var target = new ItemsControl { };
var binding = new MultiBinding
{
Bindings = new[]
{
new Binding { Path = "A", Source = source },
new Binding { Path = "B", Source = source },
new Binding { Path = "C", Source = source },
},
};
target.Bind(ItemsControl.ItemsProperty, binding);
Assert.Equal(target.ItemCount, 3);
Assert.Equal(target.Items.ElementAt(0), source.A);
Assert.Equal(target.Items.ElementAt(1), source.B);
Assert.Equal(target.Items.ElementAt(2), source.C);
}
private class ConcatConverter : IMultiValueConverter
{
public object Convert(IList<object> values, Type targetType, object parameter, CultureInfo culture)

21
tests/Avalonia.Markup.UnitTests/Extensions/IEnummerableExtension.cs

@ -0,0 +1,21 @@
using System;
using System.Collections;
namespace Avalonia.Markup.UnitTests
{
internal static class IEnumerableExtensions
{
public static object ElementAt(this IEnumerable source, int index)
{
var i = -1;
var enumerator = source.GetEnumerator();
while (enumerator.MoveNext() && ++i < index);
if (i == index)
{
return enumerator.Current;
}
throw new ArgumentOutOfRangeException(nameof(index));
}
}
}
Loading…
Cancel
Save