AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology

Home Page:https://avaloniaui.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The INotifyDataErrorInfo validates the data and vertically arranges the prompt content on the interface.

JusterZhu opened this issue · comments

Describe the bug

When I tried to implement data validation through INotifyDataErrorInfo, the following situation occurred.

9f0d769ab991404966db3d0ceb065bb

The version of the framework used is as follows.
Snipaste_2024-05-10_22-07-33

To Reproduce

The code for View and ViewModel is as follows.

View :

     <TextBox
         Grid.Row="2"
         Grid.Column="1"
         Margin="5"
         Text="{Binding PacketName}" />

View model :

public class XXViewModel : BindableBase, INotifyDataErrorInfo
{
    private string _packetName;
 
    private Dictionary<string, string> _errors = new Dictionary<string, string>();


    public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;

    public string PacketName 
    { 
        get => _packetName;
        set 
        {
            SetProperty(ref _packetName, value);
            ValidatePacketName();
        }
    }

    public bool HasErrors => _errors.Any();

    public IEnumerable GetErrors(string? propertyName)
    {
        if (string.IsNullOrEmpty(propertyName) || !_errors.ContainsKey(propertyName))
            return null;

        return _errors[propertyName];
    }

    private void ValidatePacketName()
    {
        if (string.IsNullOrEmpty(_packetName))
            _errors["PacketName"] = "Name cannot be empty.";
        else
            _errors.Remove("PacketName");

        ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs("PacketName"));
    }
}

Expected behavior

I hope that the validation error prompts are displayed horizontally, and appear in a more aesthetically pleasing way, such as in a bubble.

Avalonia version

11.0.10

OS

Windows

Additional context

No response

I've tried reducing the line height of the TextBox to force the error prompt content to be arranged horizontally, but it had no effect.

14d2a4fe67e2f04a1d4bdd28db90d39

@rabbitism help!!!

The control library I'm using is Semi , hhhhhhh~~

@rabbitism help!!!

The control library I'm using is Semi , hhhhhhh~~

please transfer issue to that repository.

But I think the way you return error message is wrong. You should return a IEnumerable<string>. If you return a single string, it will be discretized to a char array and display as you encountered.

-return _errors[propertyName];
+return new [] { _errors[propertyName] };

As mentioned above, it should be an array of errors.