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);
}
}
}
<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>