| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Controls.Metadata;
- using Avalonia.Controls.Primitives;
- using Avalonia.Media;
- using AvaloniaApplication1.Controls.Common;
- namespace AvaloniaApplication1.Controls
- {
- [TemplatePart(CS_NAME_BORDER, typeof(Border))]
- public class StickyLabel : TemplatedControl
- {
- #region *** Private Filelds ***
- private Border? _border = null;
- #endregion
- #region *** Properties ***
- private const string CS_NAME_BORDER = "PART_StickyLabel";
- public static readonly StyledProperty<string> TextProperty =
- AvaloniaProperty.Register<StickyLabel, string>(
- nameof(StickyLabel),
- defaultValue: string.Empty);
- public string Text
- {
- get => GetValue(TextProperty);
- set => SetValue(TextProperty, value);
- }
- public static readonly StyledProperty<SideOrientationEnum> SideOrientationProperty =
- AvaloniaProperty.Register<StickyLabel, SideOrientationEnum>(
- nameof(StickyLabel),
- defaultValue: SideOrientationEnum.Right);
- public SideOrientationEnum SideOrientation
- {
- get => GetValue(SideOrientationProperty);
- set => SetOrientation(value, CornerSize);
- }
- public static readonly StyledProperty<double> CornerSizeProperty =
- AvaloniaProperty.Register<StickyLabel, double>(
- nameof(StickyLabel),
- defaultValue: 10);
- public double CornerSize
- {
- get => GetValue(CornerSizeProperty);
- set => SetOrientation(SideOrientation, value);
- }
- public static readonly StyledProperty<BoxShadows> BoxShadowProperty =
- AvaloniaProperty.Register<StickyLabel, BoxShadows>(
- nameof(StickyLabel),
- defaultValue: BoxShadows.Parse("0 0 0 0 DarkGray"));
- public BoxShadows BoxShadow
- {
- get => GetValue(BoxShadowProperty);
- set => SetBoxShadow(value);
- }
- #endregion
- #region *** Constructors ***
- public StickyLabel()
- {
- }
- #endregion
- #region *** Overriden Methods ***
- protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
- {
- _border = e.NameScope.Find<Border>(CS_NAME_BORDER);
- SetOrientation(SideOrientation, CornerSize);
- }
- #endregion
- #region *** Private Methods ***
- private void SetBoxShadow(BoxShadows boxShadow)
- {
- SetValue(BoxShadowProperty, boxShadow);
- if (_border == null) return;
- _border.BoxShadow = boxShadow;
- }
- private void SetOrientation(SideOrientationEnum? sideOrientation, double? cornerSize)
- {
- if (sideOrientation != null)
- SetValue(SideOrientationProperty, sideOrientation);
- else
- sideOrientation = SideOrientation;
- if (cornerSize != null)
- SetValue(CornerSizeProperty, cornerSize);
- else
- cornerSize = CornerSize;
- if (cornerSize == null) throw new ArgumentNullException(nameof(cornerSize));
- if (_border == null) return;
- switch (sideOrientation)
- {
- case SideOrientationEnum.Left:
- _border.CornerRadius = new CornerRadius(cornerSize.GetValueOrDefault(), 0, 0, cornerSize.GetValueOrDefault());
- break;
- case SideOrientationEnum.Right:
- _border.CornerRadius = new CornerRadius(0, cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault(), 0);
- break;
- case SideOrientationEnum.Top:
- _border.CornerRadius = new CornerRadius(cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault(), 0, 0);
- break;
- case SideOrientationEnum.Down:
- _border.CornerRadius = new CornerRadius(0, 0, cornerSize.GetValueOrDefault(), cornerSize.GetValueOrDefault());
- break;
- }
- }
- #endregion
- }
- }
|