wpf

Slider Binding: Update only on Drag Ended

Parameters#

Parameter Detail
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="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="https://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>

    This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow