Files
Shrlalgo.RvKits/Melskin/Controls/Spin.xaml.cs
2026-02-12 21:29:00 +08:00

86 lines
2.5 KiB
C#

namespace Melskin.Controls;
using System.Windows;
/// <summary>
/// A spinner for displaying loading state of a page or a section.
/// </summary>
[TemplateVisualState(Name = "Spun", GroupName = "SpinStates")]
[TemplateVisualState(Name = "Unspun", GroupName = "SpinStates")]
public class Spin : ContentControl
{
#region Properties
/// <summary>
/// 获取或设置指示器属性的依赖属性。此属性用于定义旋转控件中使用的指示器元素。
/// </summary>
public static readonly DependencyProperty IndicatorProperty =
DependencyProperty.Register(
nameof(Indicator),
typeof(UIElement),
typeof(Spin),
new PropertyMetadata(null, OnSpinningChanged));
/// <summary>
/// 获取或设置指示器元素。
/// </summary>
public UIElement Indicator
{
get => (UIElement)GetValue(IndicatorProperty);
set => SetValue(IndicatorProperty, value);
}
/// <summary>
/// 获取或设置一个布尔值,指示旋转控件是否处于旋转状态。
/// </summary>
public static readonly DependencyProperty SpinningProperty =
DependencyProperty.Register(
nameof(Spinning),
typeof(bool),
typeof(Spin),
new PropertyMetadata(true, OnSpinningChanged));
/// <summary>
/// 获取或设置是否正在旋转。
/// </summary>
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); }
/// <summary>
/// 获取或设置提示文本。
/// </summary>
public static readonly DependencyProperty TipProperty =
DependencyProperty.Register(nameof(Tip), typeof(string), typeof(Spin), new PropertyMetadata(string.Empty));
/// <summary>
/// 获取或设置提示文本。
/// </summary>
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
/// <inheritdoc />
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
GoToSpinState();
}
#endregion
}