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" Width="300"
u:FormItem.Label="Email" u:FormItem.Label="Email"
Text="{Binding Email}" /> Text="{Binding Email}" />
<u:NumericDoubleUpDown Value="{Binding Number}" u:FormItem.Label="Number" Width="300"/>
</u:FormGroup> </u:FormGroup>
<u:FormItem Label="Please select a Date"> <u:FormItem Label="Please select a Date">
<CalendarDatePicker SelectedDate="{Binding Date}" /> <CalendarDatePicker SelectedDate="{Binding Date}" />

View File

@@ -24,6 +24,14 @@ public partial class DataModel : ObservableObject
get=>_name; get=>_name;
set => SetProperty(ref _name, value); 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; private string _email;

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?>( 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 public T? Value
{ {
@@ -485,6 +485,15 @@ public abstract class NumericUpDownBase<T> : NumericUpDown where T : struct, ICo
set => this.SetValue(CommandParameterProperty, value); 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) private void InvokeCommand(object? cp)
{ {
if (this.Command != null && this.Command.CanExecute(cp)) if (this.Command != null && this.Command.CanExecute(cp))