From 5c74e2c32bc401cc26b8d80f61e7e57ea4804c34 Mon Sep 17 00:00:00 2001 From: robloo Date: Sat, 13 May 2023 14:26:35 -0400 Subject: [PATCH] Add RangeBase.ValueChanged event --- .../RoutedPropertyChangedEventArgs.cs | 46 +++++++++++++++++++ src/Avalonia.Controls/Primitives/RangeBase.cs | 37 ++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/Avalonia.Base/Interactivity/RoutedPropertyChangedEventArgs.cs diff --git a/src/Avalonia.Base/Interactivity/RoutedPropertyChangedEventArgs.cs b/src/Avalonia.Base/Interactivity/RoutedPropertyChangedEventArgs.cs new file mode 100644 index 0000000000..22134b518d --- /dev/null +++ b/src/Avalonia.Base/Interactivity/RoutedPropertyChangedEventArgs.cs @@ -0,0 +1,46 @@ +namespace Avalonia.Interactivity +{ + /// + /// Provides both old and new property values with a routed event. + /// + /// The type of values. + public class RoutedPropertyChangedEventArgs : RoutedEventArgs + { + /// + /// Initializes a new instance of the class. + /// + /// The old property value. + /// The new property value. + /// The routed event associated with these event args. + public RoutedPropertyChangedEventArgs(T oldValue, T newValue, RoutedEvent? routedEvent) + : base(routedEvent) + { + OldValue = oldValue; + NewValue = newValue; + } + + /// + /// Initializes a new instance of the class. + /// + /// The old property value. + /// The new property value. + /// The routed event associated with these event args. + /// The source object that raised the routed event. + public RoutedPropertyChangedEventArgs(T oldValue, T newValue, RoutedEvent? routedEvent, object? source) + : base(routedEvent, source) + { + OldValue = oldValue; + NewValue = newValue; + } + + /// + /// Gets the old value of the property. + /// + public T OldValue { get; init; } + + /// + /// Gets the new value of the property. + /// + public T NewValue { get; init; } + } +} diff --git a/src/Avalonia.Controls/Primitives/RangeBase.cs b/src/Avalonia.Controls/Primitives/RangeBase.cs index fd9de47236..ebf7879412 100644 --- a/src/Avalonia.Controls/Primitives/RangeBase.cs +++ b/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(nameof(LargeChange), 10); /// - /// Gets or sets the minimum value. + /// Defines the event. + /// + public static readonly RoutedEvent> ValueChangedEvent = + RoutedEvent.Register>( + nameof(ValueChanged), RoutingStrategies.Bubble); + + /// + /// Occurs when the property changes. + /// + public event EventHandler>? ValueChanged + { + add => AddHandler(ValueChangedEvent, value); + remove => RemoveHandler(ValueChangedEvent, value); + } + + /// + /// Gets or sets the minimum possible value. /// public double Minimum { @@ -65,7 +82,7 @@ namespace Avalonia.Controls.Primitives } /// - /// Gets or sets the maximum value. + /// Gets or sets the maximum possible value. /// public double Maximum { @@ -104,18 +121,25 @@ namespace Avalonia.Controls.Primitives : sender.GetValue(ValueProperty); } + /// + /// Gets or sets the small increment value added or subtracted from the . + /// public double SmallChange { get => GetValue(SmallChangeProperty); set => SetValue(SmallChangeProperty, value); } + /// + /// Gets or sets the large increment value added or subtracted from the . + /// public double LargeChange { get => GetValue(LargeChangeProperty); set => SetValue(LargeChangeProperty, value); } + /// protected override void OnInitialized() { base.OnInitialized(); @@ -124,6 +148,7 @@ namespace Avalonia.Controls.Primitives CoerceValue(ValueProperty); } + /// 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( + change.GetOldValue(), + change.GetNewValue(), + ValueChangedEvent); + RaiseEvent(valueChangedEventArgs); + } } ///