Clipper.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. namespace System.Windows.Controls.Primitives
  6. {
  7. /// <summary>
  8. /// Clips a ratio of its content.
  9. /// </summary>
  10. /// <QualityBand>Preview</QualityBand>
  11. public abstract class Clipper : ContentControl
  12. {
  13. #region public double RatioVisible
  14. /// <summary>
  15. /// Gets or sets the percentage of the item visible.
  16. /// </summary>
  17. public double RatioVisible
  18. {
  19. get { return (double)GetValue(RatioVisibleProperty); }
  20. set { SetValue(RatioVisibleProperty, value); }
  21. }
  22. /// <summary>
  23. /// Identifies the RatioVisible dependency property.
  24. /// </summary>
  25. public static readonly DependencyProperty RatioVisibleProperty =
  26. DependencyProperty.Register(
  27. "RatioVisible",
  28. typeof(double),
  29. typeof(Clipper),
  30. new PropertyMetadata(1.0, OnRatioVisibleChanged));
  31. /// <summary>
  32. /// RatioVisibleProperty property changed handler.
  33. /// </summary>
  34. /// <param name="d">PartialView that changed its RatioVisible.</param>
  35. /// <param name="e">Event arguments.</param>
  36. private static void OnRatioVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  37. {
  38. Clipper source = (Clipper)d;
  39. double oldValue = (double)e.OldValue;
  40. double newValue = (double)e.NewValue;
  41. source.OnRatioVisibleChanged(oldValue, newValue);
  42. }
  43. /// <summary>
  44. /// RatioVisibleProperty property changed handler.
  45. /// </summary>
  46. /// <param name="oldValue">Old value.</param>
  47. /// <param name="newValue">New value.</param>
  48. protected virtual void OnRatioVisibleChanged(double oldValue, double newValue)
  49. {
  50. if (newValue >= 0.0 && newValue <= 1.0)
  51. {
  52. ClipContent();
  53. }
  54. else
  55. {
  56. if (newValue < 0.0)
  57. {
  58. this.RatioVisible = 0.0;
  59. }
  60. else if (newValue > 1.0)
  61. {
  62. this.RatioVisible = 1.0;
  63. }
  64. }
  65. }
  66. #endregion public double RatioVisible
  67. /// <summary>
  68. /// Initializes a new instance of the Clipper class.
  69. /// </summary>
  70. protected Clipper()
  71. {
  72. this.SizeChanged += delegate { ClipContent(); };
  73. }
  74. /// <summary>
  75. /// Updates the clip geometry.
  76. /// </summary>
  77. protected abstract void ClipContent();
  78. }
  79. }