Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding validation error is not updated properly #16937

Open
ruafelianna opened this issue Sep 5, 2024 · 6 comments
Open

Binding validation error is not updated properly #16937

ruafelianna opened this issue Sep 5, 2024 · 6 comments

Comments

@ruafelianna
Copy link

Describe the bug

I have a TextBox input which is bound to an int property in the view model. First I enter 1234567890 which is a valid int value. Then I add two more 1s: 123456789011 and see a validation error. Then I remove one 1 from the end and the validation message is updated. Then I remove another 1 and the validation message should be reset but I still see the previous validation message.

To Reproduce

  1. Create new Avalonia MVVM project.
  2. In the MainWindowViewModel add an int property:
private int _number;
public int Number
{
    get => _number;
    set => this.RaiseAndSetIfChanged(ref _number, value);
}
  1. On the MainWindow add a TextBox bound to the property:
<TextBox Width="300" Text="{Binding Number}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  1. Enter 123456789011.
  2. Remove last two 1s using Backspace.

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

изображение

@ruafelianna ruafelianna added the bug label Sep 5, 2024
@ruafelianna
Copy link
Author

ruafelianna commented Sep 5, 2024

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"/>

@ruafelianna
Copy link
Author

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:
изображение

@Hzlin7
Copy link

Hzlin7 commented Oct 28, 2024

I got the same problem. Is there any way to solve this problem?

@ruafelianna
Copy link
Author

@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).

@ruafelianna
Copy link
Author

@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.

@Ruikoto
Copy link

Ruikoto commented Nov 1, 2024

I also encountered this problem, it's frustrating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants