-
StackOverflow 文档
-
wpf 教程
-
仅在拖动结束时滑块绑定更新
-
行为实施
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);
}
}
}