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