NumericUpDown 控件看起来像是一个文本框与一对用户可单击以调整值的箭头的组合。该控件显示并设置固定的数值选择列表中的单个数值。用户可以通过单击向上和向下、按向上和向下键或在控件的文本框部件中键入一个数字来增大和减小数字。单击向上键时,值向最大值方向移动;单击向下键时,值向最小值方向移动。
我这里提供的是在网上找的别人自己写好的NumericUpDown 控件,然后我进行了样式修改,修改之后是长按向上键,值会不停的增大,直至最大值,同理,长按向下键,值会不停的减小,直至最小值(就是把以前的Button换成了RepeatButton)。单击功能仍和以前一样。
代码奉上:
首先这个是自定义控件:
public class NumericUpDown : Control
{
static NumericUpDown()
{
InitializeCommands();
// Listen to MouseLeftButtonDown event to determine if NumericUpDown should move focus to itself
EventManager.RegisterClassHandler(typeof(NumericUpDown),
Mouse.MouseDownEvent, new MouseButtonEventHandler(NumericUpDown.OnMouseLeftButtonDown), true);
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(NumericUpDown)));
}
public NumericUpDown()
: base()
{
updateValueString();
}
#region Properties
#region Value
public decimal Value
{
get { return (decimal)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
/// <summary>
/// Identifies the Value dependency property.
/// </summary>
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value", typeof(decimal), typeof(NumericUpDown),
new FrameworkPropertyMetadata(DefaultValue,
new PropertyChangedCallback(OnValueChanged),
new CoerceValueCallback(CoerceValue)
)
);
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
NumericUpDown control = (NumericUpDown)obj;
decimal oldValue = (decimal)args.OldValue;
decimal newValue = (decimal)args.NewValue;
#region Fire Automation events
NumericUpDownAutomationPeer peer = UIElementAutomationPeer.FromElement(control) as NumericUpDownAutomationPeer;
if (peer != null)
{
peer.RaiseValueChangedEvent(oldValue, newValue);
}
#endregion
RoutedPropertyChangedEventArgs<decimal> e = new RoutedPropertyChangedEventArgs<decimal>(
oldValue, newValue, ValueChangedEvent);
control.OnValueChanged(e);
control.updateValueString();
}
/// <summary>
/// Raises the ValueChanged event.
/// </summary>
/// <param name="args">Arguments associated with the ValueChanged event.</param>
protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<decimal> args)
{
RaiseEvent(args);
}
private static object CoerceValue(DependencyObject element, object value)
{
decimal newValue = (decimal)value;
NumericUpDown control = (NumericUpDown)element;
newValue = Math.Max(control.Minimum, Math.Min(control.Maximum, newValue));
newValue = Decimal.Round(newValue, control.DecimalPlaces);
return newValue;
}
#endregion
#region Minimum
public decimal Minimum
{
get { return (decimal)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register(
"Minimum", typeof(decimal), typeof(NumericUpDown),
new FrameworkPropertyMetadata(DefaultMinValue,
new PropertyChangedCallback(OnMinimumChanged), new CoerceValueCallback(CoerceMinimum)
)
);
private static void OnMinimumChanged(DependencyObject element, DependencyPropertyChangedEventArgs args)
{
element.CoerceValue(MaximumProperty);
element.CoerceValue(ValueProperty);
}
private static object CoerceMinimum(DependencyObject element, object value)
{
decimal minimum = (decimal)value;
NumericUpDown control = (NumericUpDown)element;
return Decimal.Round(minimum, control.DecimalPlaces);
}
#endregion
#region Maximum
public decimal Maximum
{
get { return (decimal)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register(
"Maximum", typeof(decimal), typeof(NumericUpDown),
new FrameworkPropertyMetadata(DefaultMaxValue,
new PropertyChangedCallback(OnMaximumChanged),
new CoerceValueCallback(CoerceMaximum)