namespace NeumUI.Controls; using System.Windows; using ContentControlBase = System.Windows.Controls.ContentControl; /// /// A spinner for displaying loading state of a page or a section. /// [TemplateVisualState(Name = "Spun", GroupName = "SpinStates")] [TemplateVisualState(Name = "Unspun", GroupName = "SpinStates")] public class Spin : ContentControlBase { #region Properties public static readonly DependencyProperty IndicatorProperty = DependencyProperty.Register( nameof(Indicator), typeof(UIElement), typeof(Spin), new PropertyMetadata(null, OnSpinningChanged)); /// /// Gets/sets element of the spinning indicator. /// public UIElement Indicator { get => (UIElement)GetValue(IndicatorProperty); set => SetValue(IndicatorProperty, value); } public static readonly DependencyProperty SpinningProperty = DependencyProperty.Register( nameof(Spinning), typeof(bool), typeof(Spin), new PropertyMetadata(true, OnSpinningChanged)); /// /// Gets/sets whether spin is spinning. /// public bool Spinning { get => (bool)GetValue(SpinningProperty); set => SetValue(SpinningProperty, value); } private static void OnSpinningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as Spin).GoToSpinState(); } private void GoToSpinState() { VisualStateManager.GoToState(this, Spinning && null == Indicator ? "Spun" : "Unspun", true); } public static readonly DependencyProperty TipProperty = DependencyProperty.Register(nameof(Tip), typeof(string), typeof(Spin), new PropertyMetadata(string.Empty)); /// /// Gets/sets customize description content when spin has children. /// public string Tip { get => (string)GetValue(TipProperty); set => SetValue(TipProperty, value); } #endregion #region Constructors static Spin() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Spin), new FrameworkPropertyMetadata(typeof(Spin))); } #endregion #region Overrides public override void OnApplyTemplate() { base.OnApplyTemplate(); GoToSpinState(); } #endregion }