/************************************************************************ Extended WPF Toolkit Copyright (C) 2010-2012 Xceed Software Inc. This program is provided to you under the terms of the Microsoft Public License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license This program can be provided to you by Xceed Software Inc. under a proprietary commercial license agreement for use in non-Open Source projects. The commercial version of Extended WPF Toolkit also includes priority technical support, commercial updates, and many additional useful WPF controls if you license Xceed Business Suite for WPF. Visit http://xceed.com and follow @datagrid on Twitter. **********************************************************************/ using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Markup; namespace Xceed.Wpf.Toolkit { /// /// Represents a spinner control that includes two Buttons. /// [TemplatePart( Name = PART_IncreaseButton, Type = typeof( ButtonBase ) )] [TemplatePart( Name = PART_DecreaseButton, Type = typeof( ButtonBase ) )] [ContentProperty( "Content" )] public class ButtonSpinner : Spinner { private const string PART_IncreaseButton = "PART_IncreaseButton"; private const string PART_DecreaseButton = "PART_DecreaseButton"; #region Properties #region AllowSpin public static readonly DependencyProperty AllowSpinProperty = DependencyProperty.Register( "AllowSpin", typeof( bool ), typeof( ButtonSpinner ), new UIPropertyMetadata( true ) ); public bool AllowSpin { get { return ( bool )GetValue( AllowSpinProperty ); } set { SetValue( AllowSpinProperty, value ); } } #endregion //AllowSpin #region Content /// /// Identifies the Content dependency property. /// 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 ); } } /// /// ContentProperty property changed handler. /// /// ButtonSpinner that changed its Content. /// Event arguments. private static void OnContentPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e ) { ButtonSpinner source = d as ButtonSpinner; source.OnContentChanged( e.OldValue, e.NewValue ); } #endregion //Content #region DecreaseButton private ButtonBase _decreaseButton; /// /// Gets or sets the DecreaseButton template part. /// private ButtonBase DecreaseButton { get { return _decreaseButton; } set { if( _decreaseButton != null ) { _decreaseButton.Click -= OnButtonClick; } _decreaseButton = value; if( _decreaseButton != null ) { _decreaseButton.Click += OnButtonClick; } } } #endregion //DecreaseButton #region IncreaseButton private ButtonBase _increaseButton; /// /// Gets or sets the IncreaseButton template part. /// private ButtonBase IncreaseButton { get { return _increaseButton; } set { if( _increaseButton != null ) { _increaseButton.Click -= OnButtonClick; } _increaseButton = value; if( _increaseButton != null ) { _increaseButton.Click += OnButtonClick; } } } #endregion //IncreaseButton #region ShowButtonSpinner public static readonly DependencyProperty ShowButtonSpinnerProperty = DependencyProperty.Register( "ShowButtonSpinner", typeof( bool ), typeof( ButtonSpinner ), new UIPropertyMetadata( true ) ); public bool ShowButtonSpinner { get { return ( bool )GetValue( ShowButtonSpinnerProperty ); } set { SetValue( ShowButtonSpinnerProperty, value ); } } #endregion //ShowButtonSpinner #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( PART_IncreaseButton ) as ButtonBase; DecreaseButton = GetTemplateChild( PART_DecreaseButton ) as ButtonBase; SetButtonUsage(); } /// /// Cancel LeftMouseButtonUp events originating from a button that has /// been changed to disabled. /// /// The data for the event. 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; } } } /// /// Called when valid spin direction changed. /// /// The old value. /// The new value. protected override void OnValidSpinDirectionChanged( ValidSpinDirections oldValue, ValidSpinDirections newValue ) { SetButtonUsage(); } #endregion //Base Class Overrides #region Event Handlers /// /// Handle click event of IncreaseButton and DecreaseButton template parts, /// translating Click to appropriate Spin event. /// /// Event sender, should be either IncreaseButton or DecreaseButton template part. /// Event args. private void OnButtonClick( object sender, RoutedEventArgs e ) { if( AllowSpin ) { SpinDirection direction = sender == IncreaseButton ? SpinDirection.Increase : SpinDirection.Decrease; OnSpin( new SpinEventArgs( direction ) ); } } #endregion //Event Handlers #region Methods /// /// Occurs when the Content property value changed. /// /// The old value of the Content property. /// The new value of the Content property. protected virtual void OnContentChanged( object oldValue, object newValue ) { } /// /// Disables or enables the buttons based on the valid spin direction. /// 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 ); } } #endregion //Methods } }