using System; using System.Windows.Controls; using Microsoft.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows; namespace Microsoft.Windows.Controls.Primitives { public abstract class UpDownBase : InputBase { #region Members /// /// Name constant for Text template part. /// internal const string ElementTextName = "TextBox"; /// /// Name constant for Spinner template part. /// internal const string ElementSpinnerName = "Spinner"; #endregion //Members #region Properties #region AllowSpin public static readonly DependencyProperty AllowSpinProperty = DependencyProperty.Register("AllowSpin", typeof(bool), typeof(UpDownBase), new UIPropertyMetadata(true)); public bool AllowSpin { get { return (bool)GetValue(AllowSpinProperty); } set { SetValue(AllowSpinProperty, value); } } #endregion //AllowSpin #region ShowButtonSpinner public static readonly DependencyProperty ShowButtonSpinnerProperty = DependencyProperty.Register("ShowButtonSpinner", typeof(bool), typeof(UpDownBase), new UIPropertyMetadata(true)); public bool ShowButtonSpinner { get { return (bool)GetValue(ShowButtonSpinnerProperty); } set { SetValue(ShowButtonSpinnerProperty, value); } } #endregion //ShowButtonSpinner protected TextBox TextBox { get; private set; } private Spinner _spinner; internal Spinner Spinner { get { return _spinner; } private set { _spinner = value; _spinner.Spin += OnSpinnerSpin; } } #endregion //Properties #region Constructors internal UpDownBase() { } #endregion //Constructors #region Base Class Overrides public override void OnApplyTemplate() { base.OnApplyTemplate(); TextBox = GetTemplateChild(ElementTextName) as TextBox; Spinner = GetTemplateChild(ElementSpinnerName) as Spinner; } protected override void OnPreviewKeyDown(KeyEventArgs e) { switch (e.Key) { case Key.Up: { if (AllowSpin) DoIncrement(); e.Handled = true; break; } case Key.Down: { if (AllowSpin) DoDecrement(); e.Handled = true; break; } case Key.Enter: { if (IsEditable) SyncTextAndValueProperties(UpDownBase.TextProperty, TextBox.Text); break; } } } protected override void OnMouseWheel(MouseWheelEventArgs e) { base.OnMouseWheel(e); if (!e.Handled && AllowSpin) { if (e.Delta < 0) { DoDecrement(); } else if (0 < e.Delta) { DoIncrement(); } e.Handled = true; } } #endregion //Base Class Overrides #region Event Handlers private void OnSpinnerSpin(object sender, SpinEventArgs e) { if (AllowSpin) OnSpin(e); } #endregion //Event Handlers #region Methods protected virtual void OnSpin(SpinEventArgs e) { if (e == null) throw new ArgumentNullException("e"); if (e.Direction == SpinDirection.Increase) DoIncrement(); else DoDecrement(); } /// /// Performs an increment if conditions allow it. /// private void DoDecrement() { if (Spinner == null || (Spinner.ValidSpinDirection & ValidSpinDirections.Decrease) == ValidSpinDirections.Decrease) { OnDecrement(); } } /// /// Performs a decrement if conditions allow it. /// private void DoIncrement() { if (Spinner == null || (Spinner.ValidSpinDirection & ValidSpinDirections.Increase) == ValidSpinDirections.Increase) { OnIncrement(); } } #region Abstract /// /// Called by OnSpin when the spin direction is SpinDirection.Increase. /// protected abstract void OnIncrement(); /// /// Called by OnSpin when the spin direction is SpinDirection.Descrease. /// protected abstract void OnDecrement(); #endregion //Abstract #endregion //Methods } }