From 2111b72bae425b143db492510915bf7e79c4330e Mon Sep 17 00:00:00 2001 From: rabbitism Date: Sat, 27 Jul 2024 14:16:28 +0800 Subject: [PATCH] fix: fix NumericUpDownBase validation propagation. --- demo/Ursa.Demo/Pages/FormDemo.axaml | 1 + demo/Ursa.Demo/ViewModels/FormDemoViewModel.cs | 8 ++++++++ src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs | 11 ++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/demo/Ursa.Demo/Pages/FormDemo.axaml b/demo/Ursa.Demo/Pages/FormDemo.axaml index 288392a..5d63392 100644 --- a/demo/Ursa.Demo/Pages/FormDemo.axaml +++ b/demo/Ursa.Demo/Pages/FormDemo.axaml @@ -41,6 +41,7 @@ Width="300" u:FormItem.Label="Email" Text="{Binding Email}" /> + diff --git a/demo/Ursa.Demo/ViewModels/FormDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/FormDemoViewModel.cs index 5f706a0..45f6ba9 100644 --- a/demo/Ursa.Demo/ViewModels/FormDemoViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/FormDemoViewModel.cs @@ -24,6 +24,14 @@ public partial class DataModel : ObservableObject get=>_name; 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; diff --git a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs index 5d41aaa..fe07226 100644 --- a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs +++ b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs @@ -380,7 +380,7 @@ public abstract class NumericUpDownBase : NumericUpDown where T : struct, ICo } public static readonly StyledProperty ValueProperty = AvaloniaProperty.Register, T?>( - nameof(Value), defaultBindingMode: BindingMode.TwoWay); + nameof(Value), defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true); public T? Value { @@ -485,6 +485,15 @@ public abstract class NumericUpDownBase : 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))