using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Tango.SharedUI; namespace Tango.SharedUI.Editors { /// /// Represents a simple numeric up down editor. /// public partial class ParameterItemNumericUpDownEditor : ParameterItemEditor { #region Constructors /// /// Initializes a new instance of the class. /// public ParameterItemNumericUpDownEditor() { InitializeComponent(); this.Loaded += ParameterItemNumericUpDownEditor_Loaded; } #endregion #region Properties /// /// Gets or sets the value. /// public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(ParameterItemNumericUpDownEditor), new PropertyMetadata(0.0, null, (d, value) => { return (d as ParameterItemNumericUpDownEditor).OnCoerceValue((double)value); })); /// /// Gets or sets the minimum value. /// public double Minimum { get { return (double)GetValue(MinimumProperty); } set { SetValue(MinimumProperty, value); } } public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(ParameterItemNumericUpDownEditor), new PropertyMetadata(0.0)); /// /// Gets or sets the maximum value. /// public double Maximum { get { return (double)GetValue(MaximumProperty); } set { SetValue(MaximumProperty, value); } } public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(ParameterItemNumericUpDownEditor), new PropertyMetadata(100.0)); /// /// Gets or sets the tick frequency for the up/down buttons. /// public double Tick { get { return (double)GetValue(TickProperty); } set { SetValue(TickProperty, value); } } public static readonly DependencyProperty TickProperty = DependencyProperty.Register("Tick", typeof(double), typeof(ParameterItemNumericUpDownEditor), new PropertyMetadata(1.0)); /// /// Gets or sets the value display format. /// public String Format { get { return (String)GetValue(FormatProperty); } set { SetValue(FormatProperty, value); } } public static readonly DependencyProperty FormatProperty = DependencyProperty.Register("Format", typeof(String), typeof(ParameterItemNumericUpDownEditor), new PropertyMetadata("0.0")); /// /// Gets or sets a value indicating whether the value is editable through keyboard. /// public bool IsReadOnly { get { return (bool)GetValue(IsReadOnlyProperty); } set { SetValue(IsReadOnlyProperty, value); } } public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(ParameterItemNumericUpDownEditor), new PropertyMetadata(false)); #endregion #region Virtual Methods /// /// Invoked before the value has changed. /// /// The value. /// protected virtual object OnCoerceValue(double value) { if (value > Maximum) return Maximum; if (value < Minimum) return Minimum; return value; } #endregion #region Event Handlers /// /// Handles the Click event of the btnUp control. /// /// The source of the event. /// The instance containing the event data. private void btnUp_Click(object sender, RoutedEventArgs e) { Value += Tick; } /// /// Handles the Click event of the btnDown control. /// /// The source of the event. /// The instance containing the event data. private void btnDown_Click(object sender, RoutedEventArgs e) { Value -= Tick; } /// /// Handles the KeyDown event of the txtValue control. /// /// The source of the event. /// The instance containing the event data. private void txtValue_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter || e.Key == Key.Escape) { BindingOperations.GetBindingExpressionBase((TextBox)txtValue, TextBox.TextProperty).UpdateSource(); } } /// /// Handles the Loaded event of the ParameterItemNumericUpDownEditor control. /// /// The source of the event. /// The instance containing the event data. private void ParameterItemNumericUpDownEditor_Loaded(object sender, RoutedEventArgs e) { if (ParameterItem != null) { Minimum = Convert.ToDouble(ParameterItem.Minimum); Maximum = Convert.ToDouble(ParameterItem.Maximum); this.Bind(ValueProperty, ParameterItem, Tango.Core.ParameterItem.ValueProperty, BindingMode.TwoWay); } } #endregion } }