From 3d327bc046ab6295fac0634b9da06fec92a8d493 Mon Sep 17 00:00:00 2001 From: Takoooooo Date: Wed, 3 Aug 2022 18:50:23 +0300 Subject: [PATCH] Fix and optimize StringFormatConverter. StringFormatConverter should validate values and return AvaloniaProperty.UnsetValue in case of incorrect value or exception. --- .../Converters/StringFormatConverter.cs | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Controls/Converters/StringFormatConverter.cs b/src/Avalonia.Controls/Converters/StringFormatConverter.cs index ae920dac7e..7075d3de99 100644 --- a/src/Avalonia.Controls/Converters/StringFormatConverter.cs +++ b/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 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; } }