Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2012 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4326429 to your computer and use it in GitHub Desktop.
Save anonymous/4326429 to your computer and use it in GitHub Desktop.
Helps find the user-selected value of a slider only when the keyboard/mouse gesture has ended.
namespace WpfApplication1.Behaviors
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
/// <summary>
/// Helps find the user-selected value of a slider only when the keyboard/mouse gesture has ended.
/// </summary>
public class SliderValueChangedBehavior : Behavior<Slider>
{
/// <summary>
/// Keys down.
/// </summary>
private int keysDown;
/// <summary>
/// Indicate whether to capture the value on latest key up.
/// </summary>
private bool applyKeyUpValue;
#region Dependency property Value
/// <summary>
/// DataBindable value.
/// </summary>
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value",
typeof(double),
typeof(SliderValueChangedBehavior),
new PropertyMetadata(default(double), OnValuePropertyChanged));
#endregion
#region Dependency property Value
/// <summary>
/// DataBindable Command
/// </summary>
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command",
typeof(ICommand),
typeof(SliderValueChangedBehavior),
new PropertyMetadata(null));
#endregion
/// <summary>
/// On behavior attached.
/// </summary>
protected override void OnAttached()
{
this.AssociatedObject.KeyUp += this.OnKeyUp;
this.AssociatedObject.KeyDown += this.OnKeyDown;
this.AssociatedObject.ValueChanged += this.OnValueChanged;
base.OnAttached();
}
/// <summary>
/// On behavior detaching.
/// </summary>
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.KeyUp -= this.OnKeyUp;
this.AssociatedObject.KeyDown -= this.OnKeyDown;
this.AssociatedObject.ValueChanged -= this.OnValueChanged;
}
/// <summary>
/// On Value dependency property change.
/// </summary>
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var me = (SliderValueChangedBehavior)d;
if (me.AssociatedObject != null)
me.Value = (double)e.NewValue;
}
/// <summary>
/// Occurs when the slider's value change.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (Mouse.Captured != null)
{
this.AssociatedObject.LostMouseCapture += this.OnLostMouseCapture;
}
else if (this.keysDown != 0)
{
this.applyKeyUpValue = true;
}
else
{
this.ApplyValue();
}
}
private void OnLostMouseCapture(object sender, MouseEventArgs e)
{
this.AssociatedObject.LostMouseCapture -= this.OnLostMouseCapture;
this.ApplyValue();
}
private void OnKeyUp(object sender, KeyEventArgs e)
{
if (this.keysDown-- != 0)
{
this.ApplyValue();
}
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
this.keysDown++;
}
/// <summary>
/// Applies the current value in the Value dependency property and raises the command.
/// </summary>
private void ApplyValue()
{
this.Value = this.AssociatedObject.Value;
if (this.Command != null)
this.Command.Execute(this.Value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment