LinearClipper.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.Windows.Media;
  6. namespace System.Windows.Controls.Primitives
  7. {
  8. /// <summary>
  9. /// Clips the content of the control in a given direction.
  10. /// </summary>
  11. /// <QualityBand>Preview</QualityBand>
  12. public class LinearClipper : Clipper
  13. {
  14. #region public ExpandDirection ExpandDirection
  15. /// <summary>
  16. /// Gets or sets the clipped edge.
  17. /// </summary>
  18. public ExpandDirection ExpandDirection
  19. {
  20. get { return (ExpandDirection) GetValue(ExpandDirectionProperty); }
  21. set { SetValue(ExpandDirectionProperty, value); }
  22. }
  23. /// <summary>
  24. /// Identifies the ExpandDirection dependency property.
  25. /// </summary>
  26. public static readonly DependencyProperty ExpandDirectionProperty =
  27. DependencyProperty.Register(
  28. "ExpandDirection",
  29. typeof(ExpandDirection),
  30. typeof(LinearClipper),
  31. new PropertyMetadata(ExpandDirection.Right, OnExpandDirectionChanged));
  32. /// <summary>
  33. /// ExpandDirectionProperty property changed handler.
  34. /// </summary>
  35. /// <param name="d">ExpandDirectionView that changed its ExpandDirection.</param>
  36. /// <param name="e">Event arguments.</param>
  37. private static void OnExpandDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  38. {
  39. LinearClipper source = (LinearClipper)d;
  40. ExpandDirection oldValue = (ExpandDirection)e.OldValue;
  41. ExpandDirection newValue = (ExpandDirection)e.NewValue;
  42. source.OnExpandDirectionChanged(oldValue, newValue);
  43. }
  44. /// <summary>
  45. /// ExpandDirectionProperty property changed handler.
  46. /// </summary>
  47. /// <param name="oldValue">Old value.</param>
  48. /// <param name="newValue">New value.</param>
  49. protected virtual void OnExpandDirectionChanged(ExpandDirection oldValue, ExpandDirection newValue)
  50. {
  51. ClipContent();
  52. }
  53. #endregion public ExpandDirection ExpandDirection
  54. /// <summary>
  55. /// Updates the clip geometry.
  56. /// </summary>
  57. protected override void ClipContent()
  58. {
  59. if (ExpandDirection == ExpandDirection.Right)
  60. {
  61. double width = this.RenderSize.Width * RatioVisible;
  62. this.Clip = new RectangleGeometry { Rect = new Rect(0, 0, width, this.RenderSize.Height) };
  63. }
  64. else if (ExpandDirection == ExpandDirection.Left)
  65. {
  66. double width = this.RenderSize.Width * RatioVisible;
  67. double rightSide = this.RenderSize.Width - width;
  68. this.Clip = new RectangleGeometry { Rect = new Rect(rightSide, 0, width, this.RenderSize.Height) };
  69. }
  70. else if (ExpandDirection == ExpandDirection.Up)
  71. {
  72. double height = this.RenderSize.Height * RatioVisible;
  73. double bottom = this.RenderSize.Height - height;
  74. this.Clip = new RectangleGeometry { Rect = new Rect(0, bottom, this.RenderSize.Width, height) };
  75. }
  76. else if (ExpandDirection == ExpandDirection.Down)
  77. {
  78. double height = this.RenderSize.Height * RatioVisible;
  79. this.Clip = new RectangleGeometry { Rect = new Rect(0, 0, this.RenderSize.Width, height) };
  80. }
  81. }
  82. }
  83. }