DataValidationViewModel.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.ComponentModel;
  3. using System.ComponentModel.DataAnnotations;
  4. using MiniMvvm;
  5. namespace ControlCatalog.ViewModels;
  6. public class DataValidationViewModel : ViewModelBase
  7. {
  8. private string? _DataAnnotationsSample;
  9. [Required]
  10. [EmailAddress]
  11. [MinLength(5)]
  12. public string? DataAnnotationsSample
  13. {
  14. get => _DataAnnotationsSample;
  15. set => RaiseAndSetIfChanged(ref _DataAnnotationsSample, value);
  16. }
  17. public Func<object, object> Converter { get; } = new Func<object, object>(o =>
  18. {
  19. return $"Error: {o}";
  20. });
  21. private string? _ExceptionInsideSetterSample;
  22. public string? ExceptionInsideSetterSample
  23. {
  24. get => _ExceptionInsideSetterSample;
  25. set
  26. {
  27. if (value is null || value.Length < 5)
  28. throw new ArgumentOutOfRangeException(nameof(value), "Give me 5 or more letter please :-)");
  29. RaiseAndSetIfChanged(ref _ExceptionInsideSetterSample, value);
  30. }
  31. }
  32. public Func<object, object> ExceptionConverter { get; } = new Func<object, object>(o =>
  33. {
  34. return o is Exception ex ? $"Huh, there was an Exception: {ex.Message}" : "Something went really wrong!";
  35. });
  36. }