fix(ui): stop NumericUpDown from writing null into non-nullable settings

Clearing the text box to type a new value sets Value to null, which the TwoWay
binding then wrote into an int/decimal target -- InvalidCastException on the
normal way of editing eight settings fields. KeepLastNumberConverter maps that
null to BindingOperations.DoNothing so the source keeps its last value.
This commit is contained in:
mika kuns
2026-08-06 10:20:53 +02:00
parent 091aca521f
commit 56f7d64f07
3 changed files with 33 additions and 8 deletions
@@ -0,0 +1,24 @@
using System.Globalization;
using Avalonia.Data;
using Avalonia.Data.Converters;
namespace ClaudeDo.Ui.Converters;
/// <summary>
/// For <c>NumericUpDown.Value</c> bound to a non-nullable numeric property. The control's Value is
/// <c>decimal?</c> and goes null the moment the text box is empty — which is exactly what happens
/// while the user clears a value to type a new one. Writing that null into an <c>int</c>/<c>decimal</c>
/// target throws <see cref="InvalidCastException"/>, so swallow it and leave the source untouched
/// until a real number arrives.
/// </summary>
public class KeepLastNumberConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value is null ? null : System.Convert.ToDecimal(value, culture);
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is null) return BindingOperations.DoNothing;
return System.Convert.ChangeType(value, Nullable.GetUnderlyingType(targetType) ?? targetType, culture);
}
}