星火批注
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZJClippingBorder.cs 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace Common.system
  6. {
  7. public class ZJClippingBorder : Border
  8. {
  9. private object _oldClip;
  10. protected override void OnRender(DrawingContext dc)
  11. {
  12. OnApplyChildClip();
  13. base.OnRender(dc);
  14. }
  15. public override UIElement Child
  16. {
  17. get => base.Child;
  18. set
  19. {
  20. if (Child != value)
  21. {
  22. if (Child != null)
  23. {
  24. Child.SetValue(ClipProperty, _oldClip);
  25. }
  26. if (value != null)
  27. {
  28. _oldClip = value.ReadLocalValue(ClipProperty);
  29. }
  30. else
  31. {
  32. // If we dont set it to null we could leak a Geometry object
  33. _oldClip = null;
  34. }
  35. base.Child = value;
  36. }
  37. }
  38. }
  39. protected virtual void OnApplyChildClip()
  40. {
  41. UIElement child = Child;
  42. if (child != null)
  43. {
  44. double top = Math.Max(CornerRadius.TopLeft, CornerRadius.TopRight);
  45. double bottom = Math.Max(CornerRadius.BottomLeft, CornerRadius.BottomRight);
  46. double max = Math.Max(top, bottom);
  47. Size size = RenderSize;
  48. double width = size.Width - (BorderThickness.Left + BorderThickness.Right);
  49. double height = size.Height - (BorderThickness.Top + BorderThickness.Bottom);
  50. Geometry result = new RectangleGeometry(new Rect(0, 0, width, height), max, max);
  51. double halfWidth = width / 2;
  52. double halfHeight = height / 2;
  53. if (CornerRadius.TopLeft == 0)
  54. {
  55. result = new CombinedGeometry(
  56. GeometryCombineMode.Union,
  57. result,
  58. new RectangleGeometry(new Rect(0, 0, halfWidth, halfHeight))
  59. );
  60. }
  61. if (CornerRadius.TopRight == 0)
  62. {
  63. result = new CombinedGeometry(GeometryCombineMode.Union, result, new RectangleGeometry
  64. (new Rect(halfWidth, 0, halfWidth, halfHeight)));
  65. }
  66. if (CornerRadius.BottomLeft == 0)
  67. {
  68. result = new CombinedGeometry
  69. (GeometryCombineMode.Union, result, new RectangleGeometry
  70. (new Rect(0, halfHeight, halfWidth, halfHeight)));
  71. }
  72. if (CornerRadius.BottomRight == 0)
  73. {
  74. result = new CombinedGeometry
  75. (
  76. GeometryCombineMode.Union,
  77. result,
  78. new RectangleGeometry(new Rect(halfWidth, halfHeight, halfWidth, halfHeight))
  79. );
  80. }
  81. child.Clip = result;
  82. }
  83. }
  84. }
  85. }