Open ruafelianna opened 2 months ago
The same happens when I use a converter. For example, if I enter a number and backspace it then there are no errors. If I enter some invalid input and backspace it then the validation error is not reset.
Also with the converter this happened in older versions too (tested in 11.0.13), but not always.
using Avalonia.Data;
using Avalonia.Data.Converters;
using System;
using System.Globalization;
namespace TestAvaloniaConverter.Converters
{
internal class SomeConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
=> value?.ToString();
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is null)
{
return null;
}
if (value is string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return null;
}
if (int.TryParse(str, out var res))
{
return res;
}
return new BindingNotification(new Exception("not int"), BindingErrorType.DataValidationError);
}
return new BindingNotification(new Exception("not string"), BindingErrorType.DataValidationError);
}
}
}
xmlns:conv="using:TestAvaloniaConverter.Converters"
...
<Window.Resources>
<conv:SomeConverter x:Key="SomeConverter" />
</Window.Resources>
<TextBox Width="300" Text="{Binding Number, Converter={StaticResource SomeConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
Also using BindingNotification
in 11.1 gives an exception. I guess the reason might be the same as in #16325.
Version 11.0.13
:
Version 11.1.3
:
I got the same problem. Is there any way to solve this problem?
@Hzlin7, as far as I can remember, for now I'm stuck with using binding to a string?
instead of a number type and then I apply some validation rules using ReactiveUI.Validation
and System.ComponentModel.INotifyDataErrorInfo
, which I also had some troubles with (I don't quite remember what the issues were, but I guess that was something related specifically to my project).
@maxkatz6, could you, please, add proper labels to the issue? I am sorry to bother you, it's just it seems like this issue was overlooked.
I also encountered this problem, it's frustrating.
This used to work correctly in Avalonia 10 but is now broken in 11.
I think BindingExpression.cs
is missing the case where a converter returns a BindingNotification with BindingErrorType.Error.
In internal override bool WriteValueToSource(object? value)
, shouldn't there be a check whether the value is a BindingNotification
in the IsDataValidationEnabled
code path.
else if (IsDataValidationEnabled)
{
var valueString = value?.ToString() ?? "(null)";
var valueTypeName = value?.GetType().FullName ?? "null";
var ex = new InvalidCastException(
$"Could not convert '{valueString}' ({valueTypeName}) to {type}.");
OnDataValidationError(ex);
return false;
}
When the converter returns a BindingNotification
, call OnDataValidationError
with the supplied exception; e.g.
else if (IsDataValidationEnabled)
{
if (value is BindingNotification be && be.ErrorType == BindingErrorType.Error)
{
OnDataValidationError(be.Error);
return false;
}
var valueString = value?.ToString() ?? "(null)";
var valueTypeName = value?.GetType().FullName ?? "null";
var ex = new InvalidCastException(
$"Could not convert '{valueString}' ({valueTypeName}) to {type}.");
OnDataValidationError(ex);
return false;
}
Describe the bug
I have a
TextBox
input which is bound to anint
property in the view model. First I enter1234567890
which is a validint
value. Then I add two more1
s:123456789011
and see a validation error. Then I remove one1
from the end and the validation message is updated. Then I remove another1
and the validation message should be reset but I still see the previous validation message.To Reproduce
int
property:TextBox
bound to the property:123456789011
.1
s usingBackspace
.Expected behavior
The validation error should be gone but it isn't.
Avalonia version
From 11.1.0-rc1 to the latest one which is currently 11.2.0-beta1.
OS
No response
Additional context