using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using Tango.Core.Commands;
using Tango.Core.EventArguments;
using Tango.Core.ExtensionMethods;
namespace Tango.Touch.Controls
{
[TemplatePart(Name = TouchNumericUpDownConrol.Number_PART, Type = typeof(TouchNumericTextBox))]
public class TouchNumericUpDownConrol: ContentControl
{
public const string Number_PART = "Number_PART";
private TouchNumericTextBox _numericValue;
private bool _changedFromAnotherControl;
#region Properties
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); IncrementCommand.RaiseCanExecuteChanged(); DecrementCommand.RaiseCanExecuteChanged(); }
}
///
/// The value property of slider
///
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(TouchNumericUpDownConrol), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnValueChanged)));
public double MinValue
{
get { return (double)GetValue(MinValueProperty); }
set { SetValue(MinValueProperty, value); }
}
///
/// The slider minimum value property
///
public static readonly DependencyProperty MinValueProperty =
DependencyProperty.Register("MinValue", typeof(double), typeof(TouchNumericUpDownConrol), new PropertyMetadata(0.0));
public double MaxValue
{
get { return (double)GetValue(MaxValueProperty); }
set { SetValue(MaxValueProperty, value); }
}
///
/// The slider maximum value property
///
public static readonly DependencyProperty MaxValueProperty =
DependencyProperty.Register("MaxValue", typeof(double), typeof(TouchNumericUpDownConrol), new PropertyMetadata(100.0));
public double Step
{
get { return (double)GetValue(StepProperty); }
set { SetValue(StepProperty, value); }
}
///
/// The slider minimum value property
///
public static readonly DependencyProperty StepProperty =
DependencyProperty.Register("Step", typeof(double), typeof(TouchNumericUpDownConrol), new PropertyMetadata(1.0));
public double NumericPartWidth
{
get { return (double)GetValue(NumericPartWidthProperty); }
set { SetValue(NumericPartWidthProperty, value); }
}
// Using a DependencyProperty as the backing store for NumericPartWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NumericPartWidthProperty =
DependencyProperty.Register("NumericPartWidth", typeof(double), typeof(TouchNumericUpDownConrol), new PropertyMetadata(54.0));
#endregion
#region Commands
public RelayCommand IncrementCommand
{
get { return (RelayCommand)GetValue(IncrementCommandProperty); }
private set { SetValue(IncrementCommandProperty, value); }
}
public static DependencyProperty IncrementCommandProperty =
DependencyProperty.Register("IncrementCommand", typeof(RelayCommand), typeof(TouchNumericUpDownConrol), new PropertyMetadata(null));
public RelayCommand DecrementCommand
{
get { return (RelayCommand)GetValue(DecrementCommandProperty); }
private set { SetValue(DecrementCommandProperty, value); }
}
public static DependencyProperty DecrementCommandProperty = DependencyProperty.Register("DecrementCommand", typeof(RelayCommand), typeof(TouchNumericUpDownConrol), new PropertyMetadata(null));
#endregion
static TouchNumericUpDownConrol()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchNumericUpDownConrol), new FrameworkPropertyMetadata(typeof(TouchNumericUpDownConrol)));
}
public TouchNumericUpDownConrol() : base()
{
IncrementCommand = new RelayCommand(()=>
{
Value += Step;
} , (x)=> { return (Value + Step) <= MaxValue; });
DecrementCommand = new RelayCommand(() => {
Value -= Step;
}, (x) => { return (Value - Step) >= MinValue; });
_changedFromAnotherControl = false;
}
#region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (_numericValue != null)
{
_numericValue.ValueChanged -= OnNumberChanged;
}
_numericValue = (TouchNumericTextBox)GetTemplateChild(Number_PART);
if (_numericValue != null)
{
// _numericValue.Value = Value;
_numericValue.ValueChanged += OnNumberChanged;
}
}
#endregion
#region Methods
private void OnNumberChanged(object sender, DoubleValueChangedEventArgs e)
{
Value = _numericValue.Value;
}
public void SetValueAnotherControl(double newValue)
{
if(Value != newValue)
{
_changedFromAnotherControl = true;
Value = newValue;
_changedFromAnotherControl = false;
}
}
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TouchNumericUpDownConrol colorPickerNumericUpDown = (TouchNumericUpDownConrol)d;
if (colorPickerNumericUpDown != null)
colorPickerNumericUpDown.OnValueNumberChanged((double?)e.OldValue, (double?)e.NewValue);
}
private void OnValueNumberChanged(double? oldValue, double? newValue)
{
if (_changedFromAnotherControl)
return;
RoutedPropertyChangedEventArgs args = new RoutedPropertyChangedEventArgs(oldValue, newValue);
args.RoutedEvent = TouchNumericUpDownConrol.ColorNumberChangedEvent;
RaiseEvent(args);
}
#endregion
#region Events
public static readonly RoutedEvent ColorNumberChangedEvent = EventManager.RegisterRoutedEvent("ColorNumberChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler), typeof(TouchNumericUpDownConrol));
public event RoutedPropertyChangedEventHandler ColorNumberChanged
{
add
{
AddHandler(ColorNumberChangedEvent, value);
}
remove
{
RemoveHandler(ColorNumberChangedEvent, value);
}
}
#endregion
}
}