TriggerBase.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Globalization;
  3. using System.Windows;
  4. using System.Windows.Markup;
  5. using System.Windows.Media.Animation;
  6. namespace HandyControl.Interactivity;
  7. [ContentProperty("Actions")]
  8. public abstract class TriggerBase : Animatable, IAttachedObject
  9. {
  10. private static readonly DependencyPropertyKey ActionsPropertyKey =
  11. DependencyProperty.RegisterReadOnly("Actions", typeof(TriggerActionCollection), typeof(TriggerBase),
  12. new FrameworkPropertyMetadata());
  13. public static readonly DependencyProperty ActionsProperty = ActionsPropertyKey.DependencyProperty;
  14. private DependencyObject _associatedObject;
  15. private readonly Type _associatedObjectTypeConstraint;
  16. internal TriggerBase(Type associatedObjectTypeConstraint)
  17. {
  18. _associatedObjectTypeConstraint = associatedObjectTypeConstraint;
  19. var actions = new TriggerActionCollection();
  20. SetValue(ActionsPropertyKey, actions);
  21. }
  22. public TriggerActionCollection Actions =>
  23. (TriggerActionCollection) GetValue(ActionsProperty);
  24. protected DependencyObject AssociatedObject
  25. {
  26. get
  27. {
  28. ReadPreamble();
  29. return _associatedObject;
  30. }
  31. }
  32. protected virtual Type AssociatedObjectTypeConstraint
  33. {
  34. get
  35. {
  36. ReadPreamble();
  37. return _associatedObjectTypeConstraint;
  38. }
  39. }
  40. public void Attach(DependencyObject dependencyObject)
  41. {
  42. if (!Equals(dependencyObject, AssociatedObject))
  43. {
  44. if (AssociatedObject != null)
  45. throw new InvalidOperationException(ExceptionStringTable
  46. .CannotHostTriggerMultipleTimesExceptionMessage);
  47. if (dependencyObject != null &&
  48. !AssociatedObjectTypeConstraint.IsInstanceOfType(dependencyObject))
  49. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
  50. ExceptionStringTable.TypeConstraintViolatedExceptionMessage,
  51. new object[]
  52. {GetType().Name, dependencyObject.GetType().Name, AssociatedObjectTypeConstraint.Name}));
  53. WritePreamble();
  54. _associatedObject = dependencyObject;
  55. WritePostscript();
  56. Actions.Attach(dependencyObject);
  57. OnAttached();
  58. }
  59. }
  60. public void Detach()
  61. {
  62. OnDetaching();
  63. WritePreamble();
  64. _associatedObject = null;
  65. WritePostscript();
  66. Actions.Detach();
  67. }
  68. DependencyObject IAttachedObject.AssociatedObject =>
  69. AssociatedObject;
  70. public event EventHandler<PreviewInvokeEventArgs> PreviewInvoke;
  71. protected override Freezable CreateInstanceCore()
  72. {
  73. return (Freezable) Activator.CreateInstance(GetType());
  74. }
  75. protected void InvokeActions(object parameter)
  76. {
  77. if (PreviewInvoke != null)
  78. {
  79. var e = new PreviewInvokeEventArgs();
  80. PreviewInvoke(this, e);
  81. if (e.Cancelling)
  82. return;
  83. }
  84. foreach (var action in Actions)
  85. action.CallInvoke(parameter);
  86. }
  87. protected virtual void OnAttached()
  88. {
  89. }
  90. protected virtual void OnDetaching()
  91. {
  92. }
  93. }