PropertyChangedBase.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. namespace Caliburn.Micro.Core {
  2. using System;
  3. using System.ComponentModel;
  4. using System.Linq.Expressions;
  5. using System.Runtime.Serialization;
  6. /// <summary>
  7. /// A base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling.
  8. /// </summary>
  9. [DataContract]
  10. public class PropertyChangedBase : INotifyPropertyChangedEx {
  11. /// <summary>
  12. /// Creates an instance of <see cref = "PropertyChangedBase" />.
  13. /// </summary>
  14. public PropertyChangedBase() {
  15. IsNotifying = true;
  16. }
  17. /// <summary>
  18. /// Occurs when a property value changes.
  19. /// </summary>
  20. public virtual event PropertyChangedEventHandler PropertyChanged;
  21. /// <summary>
  22. /// Enables/Disables property change notification.
  23. /// Virtualized in order to help with document oriented view models.
  24. /// </summary>
  25. public virtual bool IsNotifying { get; set; }
  26. /// <summary>
  27. /// Raises a change notification indicating that all bindings should be refreshed.
  28. /// </summary>
  29. public virtual void Refresh() {
  30. NotifyOfPropertyChange(string.Empty);
  31. }
  32. /// <summary>
  33. /// Notifies subscribers of the property change.
  34. /// </summary>
  35. /// <param name = "propertyName">Name of the property.</param>
  36. #if NET || SILVERLIGHT
  37. public virtual void NotifyOfPropertyChange(string propertyName) {
  38. #else
  39. public virtual void NotifyOfPropertyChange([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
  40. #endif
  41. if (IsNotifying && PropertyChanged != null) {
  42. Execute.OnUIThread(() => OnPropertyChanged(new PropertyChangedEventArgs(propertyName)));
  43. }
  44. }
  45. /// <summary>
  46. /// Notifies subscribers of the property change.
  47. /// </summary>
  48. /// <typeparam name = "TProperty">The type of the property.</typeparam>
  49. /// <param name = "property">The property expression.</param>
  50. public void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property) {
  51. NotifyOfPropertyChange(property.GetMemberInfo().Name);
  52. }
  53. /// <summary>
  54. /// Raises the <see cref="PropertyChanged" /> event directly.
  55. /// </summary>
  56. /// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>
  57. [EditorBrowsable(EditorBrowsableState.Never)]
  58. protected void OnPropertyChanged(PropertyChangedEventArgs e) {
  59. var handler = PropertyChanged;
  60. if (handler != null) {
  61. handler(this, e);
  62. }
  63. }
  64. /// <summary>
  65. /// Raises the property changed event immediately.
  66. /// </summary>
  67. /// <param name="propertyName">Name of the property.</param>
  68. public virtual void RaisePropertyChangedEventImmediately(string propertyName)
  69. {
  70. if (IsNotifying)
  71. RaisePropertyChangedEventCore(propertyName);
  72. }
  73. void RaisePropertyChangedEventCore(string propertyName)
  74. {
  75. var handler = PropertyChanged;
  76. if (handler != null)
  77. handler(this, new PropertyChangedEventArgs(propertyName));
  78. }
  79. }
  80. }