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.
The version of the framework used is as follows.
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
@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.