using Microsoft.Xaml.Behaviors; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace TeamAAS_VP.Behaviors { public class SliderUserInteractionBehavior : Behavior { public static readonly DependencyProperty UserInteractionCommandProperty = DependencyProperty.Register( nameof(UserInteractionCommand), typeof(ICommand), typeof(SliderUserInteractionBehavior)); public ICommand UserInteractionCommand { get => (ICommand)GetValue(UserInteractionCommandProperty); set => SetValue(UserInteractionCommandProperty, value); } public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register( nameof(CommandParameter), typeof(object), typeof(SliderUserInteractionBehavior)); public object CommandParameter { get => GetValue(CommandParameterProperty); set => SetValue(CommandParameterProperty, value); } private bool _isUserDragging = false; private double _previousValue; protected override void OnAttached() { base.OnAttached(); AssociatedObject.PreviewMouseDown += OnPreviewMouseDown; AssociatedObject.PreviewMouseUp += OnPreviewMouseUp; AssociatedObject.ValueChanged += OnValueChanged; } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.PreviewMouseDown -= OnPreviewMouseDown; AssociatedObject.PreviewMouseUp -= OnPreviewMouseUp; AssociatedObject.ValueChanged -= OnValueChanged; } private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { _isUserDragging = true; _previousValue = AssociatedObject.Value; } private void OnPreviewMouseUp(object sender, MouseButtonEventArgs e) { if (_isUserDragging) { _isUserDragging = false; } } private void OnValueChanged(object sender, RoutedPropertyChangedEventArgs e) { if (_isUserDragging && UserInteractionCommand?.CanExecute(CommandParameter) == true) { var parameter = new Tuple( CommandParameter, _previousValue, AssociatedObject.Value); UserInteractionCommand.Execute(parameter); _previousValue = AssociatedObject.Value; } } } }