Browse Source
Fix validation error not being cleared on valid input (#19477)
* Add failing test for binding error not being cleared, when bound value does not change
* Fix clearing binding error
pull/19488/head
Ondřej Sušovský
6 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
23 additions and
2 deletions
-
src/Avalonia.Base/Data/Core/BindingExpression.cs
-
tests/Avalonia.Base.UnitTests/Data/Core/BindingExpressionTests.DataValidation.cs
|
|
|
@ -355,8 +355,9 @@ internal partial class BindingExpression : UntypedBindingExpressionBase, IDescri |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Don't set the value if it's unchanged.
|
|
|
|
if (TypeUtilities.IdentityEquals(LeafNode.Value, value, type)) |
|
|
|
// Don't set the value if it's unchanged. If there is a binding error, we still have to set the value
|
|
|
|
// in order to clear the error.
|
|
|
|
if (TypeUtilities.IdentityEquals(LeafNode.Value, value, type) && ErrorType == BindingErrorType.None) |
|
|
|
return true; |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
@ -305,6 +305,26 @@ public partial class BindingExpressionTests |
|
|
|
BindingErrorType.DataValidationError); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Setting_Valid_Value_Should_Clear_Binding_Error() |
|
|
|
{ |
|
|
|
var data = new ViewModel { DoubleValue = 5.6 }; |
|
|
|
var target = CreateTargetWithSource( |
|
|
|
data, |
|
|
|
o => o.DoubleValue, |
|
|
|
enableDataValidation: true, |
|
|
|
mode: BindingMode.TwoWay, |
|
|
|
targetProperty: TargetClass.StringProperty); |
|
|
|
|
|
|
|
target.String = "5.6"; |
|
|
|
target.String = "5.6a"; |
|
|
|
target.String = "5.6"; |
|
|
|
|
|
|
|
AssertNoError(target, TargetClass.StringProperty); |
|
|
|
|
|
|
|
GC.KeepAlive(data); |
|
|
|
} |
|
|
|
|
|
|
|
public class ExceptionViewModel : NotifyingBase |
|
|
|
{ |
|
|
|
private int _mustBePositive; |
|
|
|
|