aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Controls/TouchNumericUpDownConrol.cs
blob: adb98d91ddee29f6e7c1b6e51be52b3410fca847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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(); }
        }
        /// <summary>
        /// The value property of slider
        /// </summary>
        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); }
        }

        /// <summary>
        /// The slider  minimum value property
        /// </summary>
        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); }
        }

        /// <summary>
        /// The slider maximum value property
        /// </summary>
        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(double), typeof(TouchNumericUpDownConrol), new PropertyMetadata(100.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 += 1;
            } , (x)=> { return (Value + 1) <= MaxValue; });
            DecrementCommand = new RelayCommand(() => {
                Value -= 1;
            }, (x) => { return (Value - 1) >= 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<double?> args = new RoutedPropertyChangedEventArgs<double?>(oldValue, newValue);
            args.RoutedEvent = TouchNumericUpDownConrol.ColorNumberChangedEvent;
            RaiseEvent(args);
        }
        
        #endregion

        #region Events

        public static readonly RoutedEvent ColorNumberChangedEvent = EventManager.RegisterRoutedEvent("ColorNumberChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<double?>), typeof(TouchNumericUpDownConrol));
        public event RoutedPropertyChangedEventHandler<double?> ColorNumberChanged
        {
            add
            {
                AddHandler(ColorNumberChangedEvent, value);
            }
            remove
            {
                RemoveHandler(ColorNumberChangedEvent, value);
            }
        }
        
        #endregion
    }
}