Browse Source
<toolkit:ButtonSpinner Spin="ButtonSpinner_Spin" >
<TextBox x:Name="_txtValue" />
</toolkit:ButtonSpinner>
private void ButtonSpinner_Spin(object sender, Microsoft.Windows.Controls.Primitives.SpinEventArgs e)
{
int value = String.IsNullOrEmpty(_txtValue.Text) ? 0 : Convert.ToInt32(_txtValue.Text);
if (e.Direction == Microsoft.Windows.Controls.Primitives.SpinDirection.Increase)
value++;
else
value--;
_txtValue.Text = value.ToString();
}
pull/1645/head
7 changed files with 502 additions and 0 deletions
@ -0,0 +1,192 @@ |
|||
using System; |
|||
using System.Windows; |
|||
using System.Windows.Input; |
|||
using System.Windows.Markup; |
|||
using System.Windows.Controls.Primitives; |
|||
using Microsoft.Windows.Controls.Primitives; |
|||
|
|||
namespace Microsoft.Windows.Controls |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a spinner control that includes two Buttons.
|
|||
/// </summary>
|
|||
[ContentProperty("Content")] |
|||
public class ButtonSpinner : Spinner |
|||
{ |
|||
#region Properties
|
|||
|
|||
#region Content
|
|||
|
|||
/// <summary>
|
|||
/// Identifies the Content dependency property.
|
|||
/// </summary>
|
|||
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(ButtonSpinner), new PropertyMetadata(null, OnContentPropertyChanged)); |
|||
public object Content |
|||
{ |
|||
get { return GetValue(ContentProperty) as object; } |
|||
set { SetValue(ContentProperty, value); } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// ContentProperty property changed handler.
|
|||
/// </summary>
|
|||
/// <param name="d">ButtonSpinner that changed its Content.</param>
|
|||
/// <param name="e">Event arguments.</param>
|
|||
private static void OnContentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|||
{ |
|||
ButtonSpinner source = d as ButtonSpinner; |
|||
source.OnContentChanged(e.OldValue, e.NewValue); |
|||
} |
|||
|
|||
#endregion //Content
|
|||
|
|||
private ButtonBase _increaseButton; |
|||
/// <summary>
|
|||
/// Gets or sets the IncreaseButton template part.
|
|||
/// </summary>
|
|||
private ButtonBase IncreaseButton |
|||
{ |
|||
get { return _increaseButton; } |
|||
set |
|||
{ |
|||
if (_increaseButton != null) |
|||
{ |
|||
_increaseButton.Click -= OnButtonClick; |
|||
} |
|||
|
|||
_increaseButton = value; |
|||
|
|||
if (_increaseButton != null) |
|||
{ |
|||
_increaseButton.Click += OnButtonClick; |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
private ButtonBase _decreaseButton; |
|||
/// <summary>
|
|||
/// Gets or sets the DecreaseButton template part.
|
|||
/// </summary>
|
|||
private ButtonBase DecreaseButton |
|||
{ |
|||
get { return _decreaseButton; } |
|||
set |
|||
{ |
|||
if (_decreaseButton != null) |
|||
{ |
|||
_decreaseButton.Click -= OnButtonClick; |
|||
} |
|||
|
|||
_decreaseButton = value; |
|||
|
|||
if (_decreaseButton != null) |
|||
{ |
|||
_decreaseButton.Click += OnButtonClick; |
|||
} |
|||
} |
|||
} |
|||
|
|||
#endregion //Properties
|
|||
|
|||
#region Constructors
|
|||
|
|||
static ButtonSpinner() |
|||
{ |
|||
DefaultStyleKeyProperty.OverrideMetadata(typeof(ButtonSpinner), new FrameworkPropertyMetadata(typeof(ButtonSpinner))); |
|||
} |
|||
|
|||
#endregion //Constructors
|
|||
|
|||
#region Base Class Overrides
|
|||
|
|||
public override void OnApplyTemplate() |
|||
{ |
|||
base.OnApplyTemplate(); |
|||
|
|||
IncreaseButton = GetTemplateChild("IncreaseButton") as ButtonBase; |
|||
DecreaseButton = GetTemplateChild("DecreaseButton") as ButtonBase; |
|||
|
|||
SetButtonUsage(); |
|||
} |
|||
|
|||
#endregion //Base Class Overrides
|
|||
|
|||
/// <summary>
|
|||
/// Occurs when the Content property value changed.
|
|||
/// </summary>
|
|||
/// <param name="oldValue">The old value of the Content property.</param>
|
|||
/// <param name="newValue">The new value of the Content property.</param>
|
|||
protected virtual void OnContentChanged(object oldValue, object newValue) { } |
|||
|
|||
/// <summary>
|
|||
/// Handle click event of IncreaseButton and DecreaseButton template parts,
|
|||
/// translating Click to appropriate Spin event.
|
|||
/// </summary>
|
|||
/// <param name="sender">Event sender, should be either IncreaseButton or DecreaseButton template part.</param>
|
|||
/// <param name="e">Event args.</param>
|
|||
private void OnButtonClick(object sender, RoutedEventArgs e) |
|||
{ |
|||
SpinDirection direction = sender == IncreaseButton ? SpinDirection.Increase : SpinDirection.Decrease; |
|||
OnSpin(new SpinEventArgs(direction)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Cancel LeftMouseButtonUp events originating from a button that has
|
|||
/// been changed to disabled.
|
|||
/// </summary>
|
|||
/// <param name="e">The data for the event.</param>
|
|||
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) |
|||
{ |
|||
base.OnMouseLeftButtonUp(e); |
|||
|
|||
Point mousePosition; |
|||
if (IncreaseButton != null && IncreaseButton.IsEnabled == false) |
|||
{ |
|||
mousePosition = e.GetPosition(IncreaseButton); |
|||
if (mousePosition.X > 0 && mousePosition.X < IncreaseButton.ActualWidth && |
|||
mousePosition.Y > 0 && mousePosition.Y < IncreaseButton.ActualHeight) |
|||
{ |
|||
e.Handled = true; |
|||
} |
|||
} |
|||
|
|||
if (DecreaseButton != null && DecreaseButton.IsEnabled == false) |
|||
{ |
|||
mousePosition = e.GetPosition(DecreaseButton); |
|||
if (mousePosition.X > 0 && mousePosition.X < DecreaseButton.ActualWidth && |
|||
mousePosition.Y > 0 && mousePosition.Y < DecreaseButton.ActualHeight) |
|||
{ |
|||
e.Handled = true; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Called when valid spin direction changed.
|
|||
/// </summary>
|
|||
/// <param name="oldValue">The old value.</param>
|
|||
/// <param name="newValue">The new value.</param>
|
|||
protected override void OnValidSpinDirectionChanged(ValidSpinDirections oldValue, ValidSpinDirections newValue) |
|||
{ |
|||
SetButtonUsage(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Disables or enables the buttons based on the valid spin direction.
|
|||
/// </summary>
|
|||
private void SetButtonUsage() |
|||
{ |
|||
// buttonspinner adds buttons that spin, so disable accordingly.
|
|||
if (IncreaseButton != null) |
|||
{ |
|||
IncreaseButton.IsEnabled = ((ValidSpinDirection & ValidSpinDirections.Increase) == ValidSpinDirections.Increase); |
|||
} |
|||
|
|||
if (DecreaseButton != null) |
|||
{ |
|||
DecreaseButton.IsEnabled = ((ValidSpinDirection & ValidSpinDirections.Decrease) == ValidSpinDirections.Decrease); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System; |
|||
|
|||
namespace Microsoft.Windows.Controls.Primitives |
|||
{ |
|||
/// <summary>
|
|||
/// Represents spin directions that could be initiated by the end-user.
|
|||
/// </summary>
|
|||
/// <QualityBand>Preview</QualityBand>
|
|||
public enum SpinDirection |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a spin initiated by the end-user in order to Increase a value.
|
|||
/// </summary>
|
|||
Increase = 0, |
|||
|
|||
/// <summary>
|
|||
/// Represents a spin initiated by the end-user in order to Decrease a value.
|
|||
/// </summary>
|
|||
Decrease = 1 |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using System; |
|||
using System.Windows; |
|||
|
|||
namespace Microsoft.Windows.Controls.Primitives |
|||
{ |
|||
/// <summary>
|
|||
/// Provides data for the Spinner.Spin event.
|
|||
/// </summary>
|
|||
/// <QualityBand>Preview</QualityBand>
|
|||
public class SpinEventArgs : RoutedEventArgs |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the SpinDirection for the spin that has been initiated by the
|
|||
/// end-user.
|
|||
/// </summary>
|
|||
public SpinDirection Direction { get; private set; } |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the SpinEventArgs class.
|
|||
/// </summary>
|
|||
/// <param name="direction">Spin direction.</param>
|
|||
public SpinEventArgs(SpinDirection direction) |
|||
: base() |
|||
{ |
|||
Direction = direction; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
using System; |
|||
using System.Windows.Controls; |
|||
using System.Windows; |
|||
|
|||
namespace Microsoft.Windows.Controls.Primitives |
|||
{ |
|||
/// <summary>
|
|||
/// Base class for controls that represents controls that can spin.
|
|||
/// </summary>
|
|||
public abstract class Spinner : Control |
|||
{ |
|||
#region Properties
|
|||
|
|||
/// <summary>
|
|||
/// Identifies the ValidSpinDirection dependency property.
|
|||
/// </summary>
|
|||
public static readonly DependencyProperty ValidSpinDirectionProperty = DependencyProperty.Register("ValidSpinDirection", typeof(ValidSpinDirections), typeof(Spinner), new PropertyMetadata(ValidSpinDirections.Increase | ValidSpinDirections.Decrease, OnValidSpinDirectionPropertyChanged)); |
|||
public ValidSpinDirections ValidSpinDirection |
|||
{ |
|||
get { return (ValidSpinDirections)GetValue(ValidSpinDirectionProperty); } |
|||
set { SetValue(ValidSpinDirectionProperty, value); } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// ValidSpinDirectionProperty property changed handler.
|
|||
/// </summary>
|
|||
/// <param name="d">ButtonSpinner that changed its ValidSpinDirection.</param>
|
|||
/// <param name="e">Event arguments.</param>
|
|||
private static void OnValidSpinDirectionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|||
{ |
|||
Spinner source = (Spinner)d; |
|||
ValidSpinDirections oldvalue = (ValidSpinDirections)e.OldValue; |
|||
ValidSpinDirections newvalue = (ValidSpinDirections)e.NewValue; |
|||
source.OnValidSpinDirectionChanged(oldvalue, newvalue); |
|||
} |
|||
|
|||
#endregion //Properties
|
|||
|
|||
/// <summary>
|
|||
/// Occurs when spinning is initiated by the end-user.
|
|||
/// </summary>
|
|||
public event EventHandler<SpinEventArgs> Spin; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the Spinner class.
|
|||
/// </summary>
|
|||
protected Spinner() { } |
|||
|
|||
/// <summary>
|
|||
/// Raises the OnSpin event when spinning is initiated by the end-user.
|
|||
/// </summary>
|
|||
/// <param name="e">Spin event args.</param>
|
|||
protected virtual void OnSpin(SpinEventArgs e) |
|||
{ |
|||
ValidSpinDirections valid = e.Direction == SpinDirection.Increase ? ValidSpinDirections.Increase : ValidSpinDirections.Decrease; |
|||
|
|||
if ((ValidSpinDirection & valid) != valid) |
|||
{ |
|||
// spin is not allowed.
|
|||
throw new InvalidOperationException("Spin action is not valid at this moment."); |
|||
} |
|||
|
|||
EventHandler<SpinEventArgs> handler = Spin; |
|||
if (handler != null) |
|||
{ |
|||
handler(this, e); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Called when valid spin direction changed.
|
|||
/// </summary>
|
|||
/// <param name="oldValue">The old value.</param>
|
|||
/// <param name="newValue">The new value.</param>
|
|||
protected virtual void OnValidSpinDirectionChanged(ValidSpinDirections oldValue, ValidSpinDirections newValue) { } |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using System; |
|||
|
|||
namespace Microsoft.Windows.Controls.Primitives |
|||
{ |
|||
/// <summary>
|
|||
/// Represents spin directions that are valid.
|
|||
/// </summary>
|
|||
[Flags] |
|||
public enum ValidSpinDirections |
|||
{ |
|||
/// <summary>
|
|||
/// Can not increase nor decrease.
|
|||
/// </summary>
|
|||
None = 0, |
|||
|
|||
/// <summary>
|
|||
/// Can increase.
|
|||
/// </summary>
|
|||
Increase = 1, |
|||
|
|||
/// <summary>
|
|||
/// Can decrease.
|
|||
/// </summary>
|
|||
Decrease = 2 |
|||
} |
|||
} |
|||
Loading…
Reference in new issue