wpfSlider Binding: Update only on Drag Ended

Parameters

ParameterDetail
Value (float)The property bound to this Dependency Property will be updated whenever the user will cease dragging the slider

Remarks

  • Make sure to reference the System.Windows.Interactivity assembly, so that the XAML Parser will recognize the xmlns:i declaration.
  • Note that the xmlns:b statement matches the namespace where the behavior implementation resides
  • Example assumes working knowledge of binding expressions and XAML.

Behavior Implementation

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Interactivity;

namespace MyBehaviorAssembly
{

public class SliderDragEndValueBehavior : Behavior<Slider>
{

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
        "Value", typeof (float), typeof (SliderDragEndValueBehavior), new PropertyMetadata(default(float)));

    public float Value
    {
        get { return (float) GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    protected override void OnAttached()
    {
        RoutedEventHandler handler = AssociatedObject_DragCompleted;
        AssociatedObject.AddHandler(Thumb.DragCompletedEvent, handler);
    }

    private void AssociatedObject_DragCompleted(object sender, RoutedEventArgs e)
    {
        Value = (float) AssociatedObject.Value;
    }

    protected override void OnDetaching()
    {
        RoutedEventHandler handler = AssociatedObject_DragCompleted;
        AssociatedObject.RemoveHandler(Thumb.DragCompletedEvent, handler);
    }
}
}

XAML Usage

<UserControl x:Class="Example.View"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                
         xmlns:b="MyBehaviorAssembly;assembly=MyBehaviorAssembly"
                      
         mc:Ignorable="d" 
         d:DesignHeight="200" d:DesignWidth="200"
               
         >
           <Slider>
            <i:Interaction.Behaviors>
                <b:SliderDragEndValueBehavior
                      
                        Value="{Binding Value, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
                        
                        />
            </i:Interaction.Behaviors>
          </Slider>

</UserControl>