ElementInvalidator.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Windows;
  2. namespace MuchInfo.Chart.WPF.Helpers
  3. {
  4. #region Enumerations
  5. public enum InvalidationType
  6. {
  7. Measure = 1,
  8. Arrange = 2,
  9. Visual = 4,
  10. ParentMeasure = 8,
  11. ParentArrange = 16
  12. }
  13. #endregion Enumerations
  14. public static class ElementInvalidator
  15. {
  16. #region Methods
  17. #region Public Static Methods
  18. public static void Invalidate(FrameworkElement target, InvalidationType invalidationTypes)
  19. {
  20. if (target != null)
  21. {
  22. if ((invalidationTypes & InvalidationType.Measure) != (InvalidationType)0)
  23. {
  24. target.InvalidateMeasure();
  25. }
  26. if ((invalidationTypes & InvalidationType.Arrange) != (InvalidationType)0)
  27. {
  28. target.InvalidateArrange();
  29. }
  30. if ((invalidationTypes & InvalidationType.Visual) != (InvalidationType)0)
  31. {
  32. target.InvalidateVisual();
  33. }
  34. var frameworkElement = target.Parent as FrameworkElement;
  35. if (frameworkElement != null)
  36. {
  37. if ((invalidationTypes & InvalidationType.ParentMeasure) != (InvalidationType)0)
  38. {
  39. frameworkElement.InvalidateMeasure();
  40. }
  41. if ((invalidationTypes & InvalidationType.ParentArrange) != (InvalidationType)0)
  42. {
  43. frameworkElement.InvalidateArrange();
  44. }
  45. }
  46. }
  47. }
  48. public static void PropertyChanged_InvalidateArrange(DependencyObject d, DependencyPropertyChangedEventArgs e)
  49. {
  50. ElementInvalidator.Invalidate(d as FrameworkElement, InvalidationType.Arrange);
  51. }
  52. public static void PropertyChanged_InvalidateMeasure(DependencyObject d, DependencyPropertyChangedEventArgs e)
  53. {
  54. ElementInvalidator.Invalidate(d as FrameworkElement, InvalidationType.Measure);
  55. }
  56. public static void PropertyChanged_InvalidateMeasureArrange(DependencyObject d, DependencyPropertyChangedEventArgs e)
  57. {
  58. ElementInvalidator.Invalidate(d as FrameworkElement, InvalidationType.Measure | InvalidationType.Arrange);
  59. }
  60. public static void PropertyChanged_InvalidateMeasureArrangeVisual(DependencyObject d, DependencyPropertyChangedEventArgs e)
  61. {
  62. ElementInvalidator.Invalidate(d as FrameworkElement, InvalidationType.Measure | InvalidationType.Arrange | InvalidationType.Visual);
  63. }
  64. public static void PropertyChanged_InvalidateMeasureArrangeVisualParentMeasureArrange(DependencyObject d, DependencyPropertyChangedEventArgs e)
  65. {
  66. ElementInvalidator.Invalidate(d as FrameworkElement, InvalidationType.Measure | InvalidationType.Arrange | InvalidationType.Visual | InvalidationType.ParentMeasure | InvalidationType.ParentArrange);
  67. }
  68. public static void PropertyChanged_InvalidateParentArrange(DependencyObject d, DependencyPropertyChangedEventArgs e)
  69. {
  70. ElementInvalidator.Invalidate(d as FrameworkElement, InvalidationType.ParentArrange);
  71. }
  72. public static void PropertyChanged_InvalidateParentMeasure(DependencyObject d, DependencyPropertyChangedEventArgs e)
  73. {
  74. ElementInvalidator.Invalidate(d as FrameworkElement, InvalidationType.ParentMeasure);
  75. }
  76. public static void PropertyChanged_InvalidateVisual(DependencyObject d, DependencyPropertyChangedEventArgs e)
  77. {
  78. ElementInvalidator.Invalidate(d as FrameworkElement, InvalidationType.Visual);
  79. }
  80. #endregion Public Static Methods
  81. #endregion Methods
  82. }
  83. }