2025-02-10 20:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using WPFluent.Input;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
|
|
namespace WPFluent.Controls;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Allows to rate positively or negatively by clicking on one of the thumbs.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ThumbRate : System.Windows.Controls.Control
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Identifies the <see cref="StateChanged"/> routed event.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static readonly RoutedEvent StateChangedEvent = EventManager.RegisterRoutedEvent(
|
|
|
|
|
|
nameof(StateChanged),
|
|
|
|
|
|
RoutingStrategy.Bubble,
|
|
|
|
|
|
typeof(TypedEventHandler<ThumbRate, RoutedEventArgs>),
|
|
|
|
|
|
typeof(ThumbRate));
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Identifies the <see cref="State"/> dependency property.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(State),
|
|
|
|
|
|
typeof(ThumbRateState),
|
|
|
|
|
|
typeof(ThumbRate),
|
|
|
|
|
|
new PropertyMetadata(ThumbRateState.None, OnStateChanged));
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Identifies the <see cref="TemplateButtonCommand"/> dependency property.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static readonly DependencyProperty TemplateButtonCommandProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(TemplateButtonCommand),
|
|
|
|
|
|
typeof(IRelayCommand),
|
|
|
|
|
|
typeof(ThumbRate),
|
|
|
|
|
|
new PropertyMetadata(null));
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="ThumbRate"/> class and attaches <see
|
|
|
|
|
|
/// cref="TemplateButtonCommand"/>.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ThumbRate()
|
|
|
|
|
|
{ SetValue(TemplateButtonCommandProperty, new RelayCommand<ThumbRateState>(OnTemplateButtonClick)); }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Occurs when <see cref="State"/> is changed.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event TypedEventHandler<ThumbRate, RoutedEventArgs> StateChanged
|
|
|
|
|
|
{
|
|
|
|
|
|
add => AddHandler(StateChangedEvent, value);
|
|
|
|
|
|
remove => RemoveHandler(StateChangedEvent, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
|
|
|
{
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (d is not ThumbRate thumbRate)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
thumbRate.OnStateChanged((ThumbRateState)e.OldValue, (ThumbRateState)e.NewValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This virtual method is called when <see cref="State"/> is changed.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected virtual void OnStateChanged(ThumbRateState previousState, ThumbRateState currentState)
|
|
|
|
|
|
{ RaiseEvent(new RoutedEventArgs(StateChangedEvent, this)); }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Triggered by clicking a button in the control template.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected virtual void OnTemplateButtonClick(ThumbRateState parameter)
|
|
|
|
|
|
{
|
2025-04-24 20:56:44 +08:00
|
|
|
|
if (State == parameter)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
SetCurrentValue(StateProperty, ThumbRateState.None);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SetCurrentValue(StateProperty, parameter);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets or sets the value determining the current state of the control.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public ThumbRateState State
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (ThumbRateState)GetValue(StateProperty);
|
|
|
|
|
|
set => SetValue(StateProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the command triggered when clicking the button.
|
|
|
|
|
|
/// </summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
internal IRelayCommand TemplateButtonCommand => (IRelayCommand)GetValue(TemplateButtonCommandProperty);
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|