Browse Source

Fix and optimize StringFormatConverter.

StringFormatConverter should validate values and return AvaloniaProperty.UnsetValue in case of incorrect value or exception.
pull/8663/head
Takoooooo 4 years ago
parent
commit
3d327bc046
  1. 26
      src/Avalonia.Controls/Converters/StringFormatConverter.cs

26
src/Avalonia.Controls/Converters/StringFormatConverter.cs

@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Avalonia.Data;
using Avalonia.Data.Converters;
namespace Avalonia.Controls.Converters;
@ -15,13 +13,25 @@ public class StringFormatConverter : IMultiValueConverter
{
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
try
{
return string.Format((string)values[0]!, values.Skip(1).ToArray());
}
catch (Exception e)
if (values != null &&
values.Count == 5 &&
values[0] is string format &&
values[1] is double &&
values[2] is double &&
values[3] is double &&
values[4] is double)
{
return new BindingNotification(e, BindingErrorType.Error);
try
{
return string.Format(format, values[1], values[2], values[3], values[4]);
}
catch
{
return AvaloniaProperty.UnsetValue;
}
}
return AvaloniaProperty.UnsetValue;
}
}

Loading…
Cancel
Save