Person.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Markup.Xaml;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Globalization;
  9. using System.Linq;
  10. using Avalonia.Media;
  11. namespace ControlCatalog.Models
  12. {
  13. public class Person : INotifyDataErrorInfo, INotifyPropertyChanged
  14. {
  15. string _firstName = string.Empty;
  16. string _lastName = string.Empty;
  17. bool _isBanned;
  18. private int _age;
  19. public string FirstName
  20. {
  21. get => _firstName;
  22. set
  23. {
  24. _firstName = value;
  25. if (string.IsNullOrWhiteSpace(value))
  26. SetError(nameof(FirstName), "First Name Required");
  27. else
  28. SetError(nameof(FirstName), null);
  29. OnPropertyChanged(nameof(FirstName));
  30. }
  31. }
  32. public string LastName
  33. {
  34. get => _lastName;
  35. set
  36. {
  37. _lastName = value;
  38. if (string.IsNullOrWhiteSpace(value))
  39. SetError(nameof(LastName), "Last Name Required");
  40. else
  41. SetError(nameof(LastName), null);
  42. OnPropertyChanged(nameof(LastName));
  43. }
  44. }
  45. public bool IsBanned
  46. {
  47. get => _isBanned;
  48. set
  49. {
  50. _isBanned = value;
  51. OnPropertyChanged(nameof(_isBanned));
  52. }
  53. }
  54. /// <summary>
  55. /// Gets or sets the age of the person
  56. /// </summary>
  57. public int Age
  58. {
  59. get => _age;
  60. set
  61. {
  62. _age = value;
  63. OnPropertyChanged(nameof(Age));
  64. }
  65. }
  66. Dictionary<string, List<string>> _errorLookup = new Dictionary<string, List<string>>();
  67. void SetError(string propertyName, string? error)
  68. {
  69. if (string.IsNullOrEmpty(error))
  70. {
  71. if (_errorLookup.Remove(propertyName))
  72. OnErrorsChanged(propertyName);
  73. }
  74. else
  75. {
  76. if (_errorLookup.TryGetValue(propertyName, out var errorList))
  77. {
  78. errorList.Clear();
  79. errorList.Add(error!);
  80. }
  81. else
  82. {
  83. var errors = new List<string> { error! };
  84. _errorLookup.Add(propertyName, errors);
  85. }
  86. OnErrorsChanged(propertyName);
  87. }
  88. }
  89. public bool HasErrors => _errorLookup.Count > 0;
  90. public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
  91. public event PropertyChangedEventHandler? PropertyChanged;
  92. void OnErrorsChanged(string propertyName)
  93. {
  94. ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
  95. }
  96. void OnPropertyChanged(string propertyName)
  97. {
  98. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  99. }
  100. public IEnumerable GetErrors(string? propertyName)
  101. {
  102. if (propertyName is { } && _errorLookup.TryGetValue(propertyName, out var errorList))
  103. return errorList;
  104. else
  105. return Array.Empty<object>();
  106. }
  107. }
  108. }