ExceptionValidationPlugin.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Data;
  4. using System;
  5. using System.Reflection;
  6. namespace Avalonia.Data.Core.Plugins
  7. {
  8. /// <summary>
  9. /// Validates properties that report errors by throwing exceptions.
  10. /// </summary>
  11. public class ExceptionValidationPlugin : IDataValidationPlugin
  12. {
  13. /// <inheritdoc/>
  14. public bool Match(WeakReference reference, string memberName) => true;
  15. /// <inheritdoc/>
  16. public IPropertyAccessor Start(WeakReference reference, string name, IPropertyAccessor inner)
  17. {
  18. return new Validator(reference, name, inner);
  19. }
  20. private class Validator : DataValidatiorBase
  21. {
  22. public Validator(WeakReference reference, string name, IPropertyAccessor inner)
  23. : base(inner)
  24. {
  25. }
  26. public override bool SetValue(object value, BindingPriority priority)
  27. {
  28. try
  29. {
  30. return base.SetValue(value, priority);
  31. }
  32. catch (TargetInvocationException ex)
  33. {
  34. Observer.OnNext(new BindingNotification(ex.InnerException, BindingErrorType.DataValidationError));
  35. }
  36. catch (Exception ex)
  37. {
  38. Observer.OnNext(new BindingNotification(ex, BindingErrorType.DataValidationError));
  39. }
  40. return false;
  41. }
  42. }
  43. }
  44. }