diff --git a/src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs b/src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs index b6b71644a3..abfbc038eb 100644 --- a/src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs +++ b/src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs @@ -91,14 +91,14 @@ namespace Avalonia.Controls /// public static readonly DirectProperty TextProperty = AvaloniaProperty.RegisterDirect(nameof(Text), o => o.Text, (o, v) => o.Text = v, - defaultBindingMode: BindingMode.TwoWay); + defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true); /// /// Defines the property. /// public static readonly DirectProperty ValueProperty = AvaloniaProperty.RegisterDirect(nameof(Value), updown => updown.Value, - (updown, v) => updown.Value = v, defaultBindingMode: BindingMode.TwoWay); + (updown, v) => updown.Value = v, defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true); /// /// Defines the property. @@ -370,6 +370,20 @@ namespace Avalonia.Controls } } + /// + /// Called to update the validation state for properties for which data validation is + /// enabled. + /// + /// The property. + /// The new binding value for the property. + protected override void UpdateDataValidation(AvaloniaProperty property, BindingValue value) + { + if (property == TextProperty || property == ValueProperty) + { + DataValidationErrors.SetError(this, value.Error); + } + } + /// /// Called when the property value changed. /// diff --git a/tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs b/tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs new file mode 100644 index 0000000000..4cef7e4d05 --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs @@ -0,0 +1,95 @@ +using System; +using System.Linq; +using System.Reactive.Subjects; +using Avalonia.Controls.Templates; +using Avalonia.Data; +using Avalonia.Threading; +using Avalonia.UnitTests; +using Xunit; + +namespace Avalonia.Controls.UnitTests +{ + public class NumericUpDownTests + { + private static TestServices Services => TestServices.StyledWindow; + + [Fact] + public void Text_Validation() + { + RunTest((control, textbox) => + { + var exception = new InvalidCastException("failed validation"); + var textObservable = new BehaviorSubject(new BindingNotification(exception, BindingErrorType.DataValidationError)); + control.Bind(NumericUpDown.TextProperty, textObservable); + Dispatcher.UIThread.RunJobs(); + + Assert.True(DataValidationErrors.GetHasErrors(control)); + Assert.True(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception })); + }); + } + + [Fact] + public void Value_Validation() + { + RunTest((control, textbox) => + { + var exception = new InvalidCastException("failed validation"); + var valueObservable = new BehaviorSubject(new BindingNotification(exception, BindingErrorType.DataValidationError)); + control.Bind(NumericUpDown.ValueProperty, valueObservable); + Dispatcher.UIThread.RunJobs(); + + Assert.True(DataValidationErrors.GetHasErrors(control)); + Assert.True(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception })); + }); + } + + private void RunTest(Action test) + { + using (UnitTestApplication.Start(Services)) + { + var control = CreateControl(); + TextBox textBox = GetTextBox(control); + var window = new Window { Content = control }; + window.ApplyTemplate(); + window.Presenter.ApplyTemplate(); + Dispatcher.UIThread.RunJobs(); + test.Invoke(control, textBox); + } + } + + private NumericUpDown CreateControl() + { + var control = new NumericUpDown + { + Template = CreateTemplate() + }; + + control.ApplyTemplate(); + return control; + } + private TextBox GetTextBox(NumericUpDown control) + { + return control.GetTemplateChildren() + .OfType() + .Select(b => b.Content) + .OfType() + .First(); + } + private IControlTemplate CreateTemplate() + { + return new FuncControlTemplate((control, scope) => + { + var textBox = + new TextBox + { + Name = "PART_TextBox" + }.RegisterInNameScope(scope); + return new ButtonSpinner + { + Name = "PART_Spinner", + Content = textBox, + }.RegisterInNameScope(scope); + }); + } + } +}