Browse Source

Support StringFormat without Converter in MultiBinding

pull/2598/head
OronDF343 7 years ago
parent
commit
b519ecc121
  1. 46
      src/Avalonia.Base/Data/Converters/StringFormatMultiValueConverter.cs
  2. 23
      src/Markup/Avalonia.Markup/Data/MultiBinding.cs
  3. 40
      tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests_Converters.cs

46
src/Avalonia.Base/Data/Converters/StringFormatMultiValueConverter.cs

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Avalonia.Data.Converters
{
/// <summary>
/// A multi-value converter which calls <see cref="string.Format(string, object)"/>
/// </summary>
public class StringFormatMultiValueConverter : IMultiValueConverter
{
/// <summary>
/// Initializes a new instance of the <see cref="StringFormatMultiValueConverter"/> class.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="inner">
/// An optional inner converter to be called before the format takes place.
/// </param>
public StringFormatMultiValueConverter(string format, IMultiValueConverter inner)
{
Contract.Requires<ArgumentNullException>(format != null);
Format = format;
Inner = inner;
}
/// <summary>
/// Gets an inner value converter which will be called before the string format takes place.
/// </summary>
public IMultiValueConverter Inner { get; }
/// <summary>
/// Gets the format string.
/// </summary>
public string Format { get; }
/// <inheritdoc/>
public object Convert(IList<object> values, Type targetType, object parameter, CultureInfo culture)
{
return Inner == null
? string.Format(culture, Format, values.ToArray())
: string.Format(culture, Format, Inner.Convert(values, targetType, parameter, culture));
}
}
}

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

@ -64,16 +64,20 @@ namespace Avalonia.Data
object anchor = null,
bool enableDataValidation = false)
{
if (Converter == null)
{
throw new NotSupportedException("MultiBinding without Converter not currently supported.");
}
var targetType = targetProperty?.PropertyType ?? typeof(object);
var children = Bindings.Select(x => x.Initiate(target, null));
var input = children.Select(x => x.Observable).CombineLatest().Select(x => ConvertValue(x, targetType));
var mode = Mode == BindingMode.Default ?
targetProperty?.GetMetadata(target.GetType()).DefaultBindingMode : Mode;
// 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) &&
(targetType == typeof(string) || targetType == typeof(object)))
{
Converter = new StringFormatMultiValueConverter(StringFormat, Converter);
}
switch (mode)
{
@ -97,15 +101,6 @@ namespace Avalonia.Data
converted = FallbackValue;
}
// 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) &&
(targetType == typeof(string) || targetType == typeof(object)))
{
converted = string.Format(culture, StringFormat, converted);
}
return converted;
}
}

40
tests/Avalonia.Markup.UnitTests/Data/MultiBindingTests_Converters.cs

@ -5,11 +5,10 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Avalonia.Data.Core;
using Avalonia.Layout;
using Xunit;
namespace Avalonia.Markup.UnitTests.Data
@ -21,7 +20,30 @@ namespace Avalonia.Markup.UnitTests.Data
{
var textBlock = new TextBlock
{
DataContext = new MultiBindingTests_Converters.Class1(),
DataContext = new Class1(),
};
var target = new MultiBinding
{
StringFormat = "{0:0.0} + {1:00}",
Bindings =
{
new Binding(nameof(Class1.Foo)),
new Binding(nameof(Class1.Bar)),
}
};
textBlock.Bind(TextBlock.TextProperty, target);
Assert.Equal("1.0 + 02", textBlock.Text);
}
[Fact]
public void StringFormat_Should_Be_Applied_After_Converter()
{
var textBlock = new TextBlock
{
DataContext = new Class1(),
};
var target = new MultiBinding
@ -30,8 +52,8 @@ namespace Avalonia.Markup.UnitTests.Data
Converter = new SumOfDoublesConverter(),
Bindings =
{
new Binding(nameof(MultiBindingTests_Converters.Class1.Foo)),
new Binding(nameof(MultiBindingTests_Converters.Class1.Bar)),
new Binding(nameof(Class1.Foo)),
new Binding(nameof(Class1.Bar)),
}
};
@ -45,7 +67,7 @@ namespace Avalonia.Markup.UnitTests.Data
{
var textBlock = new TextBlock
{
DataContext = new MultiBindingTests_Converters.Class1(),
DataContext = new Class1(),
};
var target = new MultiBinding
@ -54,12 +76,12 @@ namespace Avalonia.Markup.UnitTests.Data
Converter = new SumOfDoublesConverter(),
Bindings =
{
new Binding(nameof(MultiBindingTests_Converters.Class1.Foo)),
new Binding(nameof(MultiBindingTests_Converters.Class1.Bar)),
new Binding(nameof(Class1.Foo)),
new Binding(nameof(Class1.Bar)),
}
};
textBlock.Bind(TextBlock.WidthProperty, target);
textBlock.Bind(Layoutable.WidthProperty, target);
Assert.Equal(3.0, textBlock.Width);
}

Loading…
Cancel
Save