StickyLabel.axaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Metadata;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Media;
  7. using AvaloniaApplication1.Controls.Common;
  8. namespace AvaloniaApplication1.Controls
  9. {
  10. [TemplatePart(CS_NAME_BORDER, typeof(Border))]
  11. public class StickyLabel : TemplatedControl
  12. {
  13. #region *** Private Filelds ***
  14. private Border? _border = null;
  15. #endregion
  16. #region *** Properties ***
  17. private const string CS_NAME_BORDER = "PART_StickyLabel";
  18. public static readonly StyledProperty<string> TextProperty =
  19. AvaloniaProperty.Register<StickyLabel, string>(
  20. nameof(StickyLabel),
  21. defaultValue: string.Empty);
  22. public string Text
  23. {
  24. get => GetValue(TextProperty);
  25. set => SetValue(TextProperty, value);
  26. }
  27. public static readonly StyledProperty<SideOrientationEnum> SideOrientationProperty =
  28. AvaloniaProperty.Register<StickyLabel, SideOrientationEnum>(
  29. nameof(StickyLabel),
  30. defaultValue: SideOrientationEnum.Right);
  31. public SideOrientationEnum SideOrientation
  32. {
  33. get => GetValue(SideOrientationProperty);
  34. set => SetOrientation(value, CornerSize);
  35. }
  36. public static readonly StyledProperty<double> CornerSizeProperty =
  37. AvaloniaProperty.Register<StickyLabel, double>(
  38. nameof(StickyLabel),
  39. defaultValue: 10);
  40. public double CornerSize
  41. {
  42. get => GetValue(CornerSizeProperty);
  43. set => SetOrientation(SideOrientation, value);
  44. }
  45. public static readonly StyledProperty<BoxShadows> BoxShadowProperty =
  46. AvaloniaProperty.Register<StickyLabel, BoxShadows>(
  47. nameof(StickyLabel),
  48. defaultValue: BoxShadows.Parse("0 0 0 0 DarkGray"));
  49. public BoxShadows BoxShadow
  50. {
  51. get => GetValue(BoxShadowProperty);
  52. set => SetBoxShadow(value);
  53. }
  54. #endregion
  55. #region *** Constructors ***
  56. public StickyLabel()
  57. {
  58. }
  59. #endregion
  60. #region *** Overriden Methods ***
  61. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  62. {
  63. _border = e.NameScope.Find<Border>(CS_NAME_BORDER);
  64. SetOrientation(SideOrientation, CornerSize);
  65. }
  66. #endregion
  67. #region *** Private Methods ***
  68. private void SetBoxShadow(BoxShadows boxShadow)
  69. {
  70. SetValue(BoxShadowProperty, boxShadow);
  71. if (_border == null) return;
  72. _border.BoxShadow = boxShadow;
  73. }
  74. private void SetOrientation(SideOrientationEnum? sideOrientation, double? cornerSize)
  75. {
  76. if (sideOrientation != null)
  77. SetValue(SideOrientationProperty, sideOrientation);
  78. else
  79. sideOrientation = SideOrientation;
  80. if (cornerSize != null)
  81. SetValue(CornerSizeProperty, cornerSize);
  82. else
  83. cornerSize = CornerSize;
  84. if (cornerSize == null) throw new ArgumentNullException(nameof(cornerSize));
  85. if (_border == null) return;
  86. switch (sideOrientation)
  87. {
  88. case SideOrientationEnum.Left:
  89. _border.CornerRadius = new CornerRadius(cornerSize.GetValueOrDefault(), 0, 0, cornerSize.GetValueOrDefault());
  90. break;
  91. case SideOrientationEnum.Right:
  92. _border.CornerRadius = new CornerRadius(0, cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault(), 0);
  93. break;
  94. case SideOrientationEnum.Top:
  95. _border.CornerRadius = new CornerRadius(cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault(), 0, 0);
  96. break;
  97. case SideOrientationEnum.Down:
  98. _border.CornerRadius = new CornerRadius(0, 0, cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault());
  99. break;
  100. }
  101. }
  102. #endregion
  103. }
  104. }