Browse Source

Always call UpdateDataValidation on notification.

This speeds up the common path of no data validation: this way we don't
need to get the metadata unless a binding notification is received. This
means that if a binding with data validation is assigned to a property
that isn't interested then UpdateDataValidation will be called, but the
control should just ignore it.
pull/691/head
Steven Kirk 10 years ago
parent
commit
4d0d7da969
  1. 4
      src/Avalonia.Base/AvaloniaObject.cs
  2. 5
      src/Avalonia.Controls/TextBox.cs
  3. 10
      tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_DataValidation.cs

4
src/Avalonia.Base/AvaloniaObject.cs

@ -658,7 +658,6 @@ namespace Avalonia
/// <param name="value">The value.</param>
private void SetDirectValue(AvaloniaProperty property, object value)
{
var metadata = (IDirectPropertyMetadata)property.GetMetadata(GetType());
var notification = value as BindingNotification;
if (notification != null)
@ -682,6 +681,7 @@ namespace Avalonia
if (notification == null || notification.HasValue)
{
var metadata = (IDirectPropertyMetadata)property.GetMetadata(GetType());
var accessor = (IDirectPropertyAccessor)GetRegistered(property);
var finalValue = value == AvaloniaProperty.UnsetValue ?
metadata.UnsetValue : value;
@ -691,7 +691,7 @@ namespace Avalonia
accessor.SetValue(this, finalValue);
}
if (metadata.EnableDataValidation && notification != null)
if (notification != null)
{
UpdateDataValidation(property, notification);
}

5
src/Avalonia.Controls/TextBox.cs

@ -464,7 +464,10 @@ namespace Avalonia.Controls
protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification status)
{
((IPseudoClasses)Classes).Set(":error", status.ErrorType != BindingErrorType.None);
if (property == TextProperty)
{
((IPseudoClasses)Classes).Set(":error", status.ErrorType != BindingErrorType.None);
}
}
private int CoerceCaretIndex(int value)

10
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_DataValidation.cs

@ -14,10 +14,7 @@ namespace Avalonia.Base.UnitTests
{
var target = new Class1();
target.SetValue(Class1.NonValidatedProperty, new BindingNotification(6));
target.SetValue(Class1.NonValidatedProperty, new BindingNotification(new Exception(), BindingErrorType.Error));
target.SetValue(Class1.NonValidatedProperty, new BindingNotification(new Exception(), BindingErrorType.DataValidationError));
target.SetValue(Class1.NonValidatedProperty, new BindingNotification(7));
target.SetValue(Class1.NonValidatedDirectProperty, 6);
Assert.Empty(target.Notifications);
}
@ -27,10 +24,7 @@ namespace Avalonia.Base.UnitTests
{
var target = new Class1();
target.SetValue(Class1.NonValidatedDirectProperty, new BindingNotification(6));
target.SetValue(Class1.NonValidatedDirectProperty, new BindingNotification(new Exception(), BindingErrorType.Error));
target.SetValue(Class1.NonValidatedDirectProperty, new BindingNotification(new Exception(), BindingErrorType.DataValidationError));
target.SetValue(Class1.NonValidatedDirectProperty, new BindingNotification(7));
target.SetValue(Class1.NonValidatedDirectProperty, 6);
Assert.Empty(target.Notifications);
}

Loading…
Cancel
Save