RoutedPropertyChangingEventArgs.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993] for details.
  4. // All other rights reserved.
  5. using System;
  6. using System.Windows;
  7. namespace System.Windows.Controls
  8. {
  9. /// <summary>
  10. /// Provides event data for various routed events that track property values
  11. /// changing. Typically the events denote a cancellable action.
  12. /// </summary>
  13. /// <typeparam name="T">
  14. /// The type of the value for the dependency property that is changing.
  15. /// </typeparam>
  16. /// <QualityBand>Preview</QualityBand>
  17. public class RoutedPropertyChangingEventArgs<T> : RoutedEventArgs
  18. {
  19. /// <summary>
  20. /// Gets the <see cref="T:System.Windows.DependencyProperty" />
  21. /// identifier for the property that is changing.
  22. /// </summary>
  23. /// <value>
  24. /// The <see cref="T:System.Windows.DependencyProperty" /> identifier
  25. /// for the property that is changing.
  26. /// </value>
  27. public DependencyProperty Property { get; private set; }
  28. /// <summary>
  29. /// Gets a value that reports the previous value of the changing
  30. /// property.
  31. /// </summary>
  32. /// <value>
  33. /// The previous value of the changing property.
  34. /// </value>
  35. public T OldValue { get; private set; }
  36. /// <summary>
  37. /// Gets or sets a value that reports the new value of the changing
  38. /// property, assuming that the property change is not cancelled.
  39. /// </summary>
  40. /// <value>
  41. /// The new value of the changing property.
  42. /// </value>
  43. public T NewValue { get; set; }
  44. /// <summary>
  45. /// Gets a value indicating whether the property change that originated
  46. /// the RoutedPropertyChanging event is cancellable.
  47. /// </summary>
  48. /// <value>
  49. /// True if the property change is cancellable. false if the property
  50. /// change is not cancellable.
  51. /// </value>
  52. public bool IsCancelable { get; private set; }
  53. /// <summary>
  54. /// Gets or sets a value indicating whether the property change that
  55. /// originated the RoutedPropertyChanging event should be cancelled.
  56. /// </summary>
  57. /// <value>
  58. /// True to cancel the property change; this resets the property to
  59. /// <see cref="P:System.Windows.Controls.RoutedPropertyChangingEventArgs`1.OldValue" />.
  60. /// false to not cancel the property change; the value changes to
  61. /// <see cref="P:System.Windows.Controls.RoutedPropertyChangingEventArgs`1.NewValue" />.
  62. /// </value>
  63. /// <exception cref="T:System.InvalidOperationException">
  64. /// Attempted to cancel in an instance where
  65. /// <see cref="P:System.Windows.Controls.RoutedPropertyChangingEventArgs`1.IsCancelable" />
  66. /// is false.
  67. /// </exception>
  68. public bool Cancel
  69. {
  70. get { return _cancel; }
  71. set
  72. {
  73. if (IsCancelable)
  74. {
  75. _cancel = value;
  76. }
  77. else if (value)
  78. {
  79. throw new InvalidOperationException(Muchinfo.WPF.Controls.Properties.Resources.RoutedPropertyChangingEventArgs_CancelSet_InvalidOperation);
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Private member variable for Cancel property.
  85. /// </summary>
  86. private bool _cancel;
  87. /// <summary>
  88. /// Gets or sets a value indicating whether internal value coercion is
  89. /// acting on the property change that originated the
  90. /// RoutedPropertyChanging event.
  91. /// </summary>
  92. /// <value>
  93. /// True if coercion is active. false if coercion is not active.
  94. /// </value>
  95. /// <remarks>
  96. /// This is a total hack to work around the class hierarchy for Value
  97. /// coercion in NumericUpDown.
  98. /// </remarks>
  99. public bool InCoercion { get; set; }
  100. /// <summary>
  101. /// Initializes a new instance of the
  102. /// <see cref="T:System.Windows.Controls.RoutedPropertyChangingEventArgs`1" />
  103. /// class.
  104. /// </summary>
  105. /// <param name="property">
  106. /// The <see cref="T:System.Windows.DependencyProperty" /> identifier
  107. /// for the property that is changing.
  108. /// </param>
  109. /// <param name="oldValue">The previous value of the property.</param>
  110. /// <param name="newValue">
  111. /// The new value of the property, assuming that the property change is
  112. /// not cancelled.
  113. /// </param>
  114. /// <param name="isCancelable">
  115. /// True if the property change is cancellable by setting
  116. /// <see cref="P:System.Windows.Controls.RoutedPropertyChangingEventArgs`1.Cancel" />
  117. /// to true in event handling. false if the property change is not
  118. /// cancellable.
  119. /// </param>
  120. public RoutedPropertyChangingEventArgs(
  121. DependencyProperty property,
  122. T oldValue,
  123. T newValue,
  124. bool isCancelable)
  125. {
  126. Property = property;
  127. OldValue = oldValue;
  128. NewValue = newValue;
  129. IsCancelable = isCancelable;
  130. Cancel = false;
  131. }
  132. #if !SILVERLIGHT
  133. /// <summary>
  134. /// Initializes a new instance of the
  135. /// <see cref="T:System.Windows.Controls.RoutedPropertyChangingEventArgs`1" />
  136. /// class.
  137. /// </summary>
  138. /// <param name="property">
  139. /// The <see cref="T:System.Windows.DependencyProperty" /> identifier
  140. /// for the property that is changing.
  141. /// </param>
  142. /// <param name="oldValue">The previous value of the property.</param>
  143. /// <param name="newValue">
  144. /// The new value of the property, assuming that the property change is
  145. /// not cancelled.
  146. /// </param>
  147. /// <param name="isCancelable">
  148. /// True if the property change is cancellable by setting
  149. /// <see cref="P:System.Windows.Controls.RoutedPropertyChangingEventArgs`1.Cancel" />
  150. /// to true in event handling. false if the property change is not
  151. /// cancellable.
  152. /// </param>
  153. /// <param name="routedEvent">The routed event identifier for this instance.</param>
  154. public RoutedPropertyChangingEventArgs(DependencyProperty property,
  155. T oldValue, T newValue, bool isCancelable, RoutedEvent routedEvent)
  156. : base(routedEvent)
  157. {
  158. Property = property;
  159. OldValue = oldValue;
  160. NewValue = newValue;
  161. IsCancelable = isCancelable;
  162. Cancel = false;
  163. }
  164. #endif
  165. }
  166. }