Merge pull request #314 from irihitech/numeirc-validation

Fix: fix numeric updown validation error display issue
This commit is contained in:
Dong Bin
2024-07-27 15:21:42 +08:00
committed by GitHub
3 changed files with 19 additions and 1 deletions

View File

@@ -41,6 +41,7 @@
Width="300"
u:FormItem.Label="Email"
Text="{Binding Email}" />
<u:NumericDoubleUpDown Value="{Binding Number}" u:FormItem.Label="Number" Width="300"/>
</u:FormGroup>
<u:FormItem Label="Please select a Date">
<CalendarDatePicker SelectedDate="{Binding Date}" />

View File

@@ -25,6 +25,14 @@ public partial class DataModel : ObservableObject
set => SetProperty(ref _name, value);
}
private double _number;
[Range(0.0, 10.0)]
public double Number
{
get => _number;
set => SetProperty(ref _number, value);
}
private string _email;
[EmailAddress]

View File

@@ -380,7 +380,7 @@ public abstract class NumericUpDownBase<T> : NumericUpDown where T : struct, ICo
}
public static readonly StyledProperty<T?> ValueProperty = AvaloniaProperty.Register<NumericUpDownBase<T>, T?>(
nameof(Value), defaultBindingMode: BindingMode.TwoWay);
nameof(Value), defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true);
public T? Value
{
@@ -485,6 +485,15 @@ public abstract class NumericUpDownBase<T> : NumericUpDown where T : struct, ICo
set => this.SetValue(CommandParameterProperty, value);
}
protected override void UpdateDataValidation(AvaloniaProperty property, BindingValueType state, Exception? error)
{
if (property == ValueProperty)
{
DataValidationErrors.SetError(this, error);
}
}
private void InvokeCommand(object? cp)
{
if (this.Command != null && this.Command.CanExecute(cp))