Browse Source

Add RangeBase.ValueChanged event

pull/11362/head
robloo 3 years ago
parent
commit
5c74e2c32b
  1. 46
      src/Avalonia.Base/Interactivity/RoutedPropertyChangedEventArgs.cs
  2. 37
      src/Avalonia.Controls/Primitives/RangeBase.cs

46
src/Avalonia.Base/Interactivity/RoutedPropertyChangedEventArgs.cs

@ -0,0 +1,46 @@
namespace Avalonia.Interactivity
{
/// <summary>
/// Provides both old and new property values with a routed event.
/// </summary>
/// <typeparam name="T">The type of values.</typeparam>
public class RoutedPropertyChangedEventArgs<T> : RoutedEventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="RoutedPropertyChangedEventArgs{T}"/> class.
/// </summary>
/// <param name="oldValue">The old property value.</param>
/// <param name="newValue">The new property value.</param>
/// <param name="routedEvent">The routed event associated with these event args.</param>
public RoutedPropertyChangedEventArgs(T oldValue, T newValue, RoutedEvent? routedEvent)
: base(routedEvent)
{
OldValue = oldValue;
NewValue = newValue;
}
/// <summary>
/// Initializes a new instance of the <see cref="RoutedPropertyChangedEventArgs{T}"/> class.
/// </summary>
/// <param name="oldValue">The old property value.</param>
/// <param name="newValue">The new property value.</param>
/// <param name="routedEvent">The routed event associated with these event args.</param>
/// <param name="source">The source object that raised the routed event.</param>
public RoutedPropertyChangedEventArgs(T oldValue, T newValue, RoutedEvent? routedEvent, object? source)
: base(routedEvent, source)
{
OldValue = oldValue;
NewValue = newValue;
}
/// <summary>
/// Gets the old value of the property.
/// </summary>
public T OldValue { get; init; }
/// <summary>
/// Gets the new value of the property.
/// </summary>
public T NewValue { get; init; }
}
}

37
src/Avalonia.Controls/Primitives/RangeBase.cs

@ -1,5 +1,6 @@
using System;
using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.Utilities;
namespace Avalonia.Controls.Primitives
@ -42,7 +43,23 @@ namespace Avalonia.Controls.Primitives
AvaloniaProperty.Register<RangeBase, double>(nameof(LargeChange), 10);
/// <summary>
/// Gets or sets the minimum value.
/// Defines the <see cref="ValueChanged"/> event.
/// </summary>
public static readonly RoutedEvent<RoutedPropertyChangedEventArgs<double>> ValueChangedEvent =
RoutedEvent.Register<RangeBase, RoutedPropertyChangedEventArgs<double>>(
nameof(ValueChanged), RoutingStrategies.Bubble);
/// <summary>
/// Occurs when the <see cref="Value"/> property changes.
/// </summary>
public event EventHandler<RoutedPropertyChangedEventArgs<double>>? ValueChanged
{
add => AddHandler(ValueChangedEvent, value);
remove => RemoveHandler(ValueChangedEvent, value);
}
/// <summary>
/// Gets or sets the minimum possible value.
/// </summary>
public double Minimum
{
@ -65,7 +82,7 @@ namespace Avalonia.Controls.Primitives
}
/// <summary>
/// Gets or sets the maximum value.
/// Gets or sets the maximum possible value.
/// </summary>
public double Maximum
{
@ -104,18 +121,25 @@ namespace Avalonia.Controls.Primitives
: sender.GetValue(ValueProperty);
}
/// <summary>
/// Gets or sets the small increment value added or subtracted from the <see cref="Value"/>.
/// </summary>
public double SmallChange
{
get => GetValue(SmallChangeProperty);
set => SetValue(SmallChangeProperty, value);
}
/// <summary>
/// Gets or sets the large increment value added or subtracted from the <see cref="Value"/>.
/// </summary>
public double LargeChange
{
get => GetValue(LargeChangeProperty);
set => SetValue(LargeChangeProperty, value);
}
/// <inheritdoc/>
protected override void OnInitialized()
{
base.OnInitialized();
@ -124,6 +148,7 @@ namespace Avalonia.Controls.Primitives
CoerceValue(ValueProperty);
}
/// <inheritdoc/>
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
@ -136,6 +161,14 @@ namespace Avalonia.Controls.Primitives
{
OnMaximumChanged();
}
else if (change.Property == ValueProperty)
{
var valueChangedEventArgs = new RoutedPropertyChangedEventArgs<double>(
change.GetOldValue<double>(),
change.GetNewValue<double>(),
ValueChangedEvent);
RaiseEvent(valueChangedEventArgs);
}
}
/// <summary>

Loading…
Cancel
Save