forked from Stopbyte/WPF-Numeric-Spinner-NumericUpDown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumericSpinner.xaml.cs
184 lines (152 loc) · 5.43 KB
/
NumericSpinner.xaml.cs
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
181
182
183
184
using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
namespace Stopbyte.Controls
{
/// <summary>
/// Interaction logic for NumericSpinner.xaml
/// </summary>
public partial class NumericSpinner : UserControl
{
#region Fields
public event EventHandler PropertyChanged;
public event EventHandler ValueChanged;
#endregion
public NumericSpinner()
{
InitializeComponent();
tb_main.SetBinding(TextBox.TextProperty, new Binding("Value")
{
ElementName = "root_numeric_spinner",
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
DependencyPropertyDescriptor.FromProperty(ValueProperty, typeof(NumericSpinner)).AddValueChanged(this, PropertyChanged);
DependencyPropertyDescriptor.FromProperty(ValueProperty, typeof(NumericSpinner)).AddValueChanged(this, ValueChanged);
DependencyPropertyDescriptor.FromProperty(DecimalsProperty, typeof(NumericSpinner)).AddValueChanged(this, PropertyChanged);
DependencyPropertyDescriptor.FromProperty(MinValueProperty, typeof(NumericSpinner)).AddValueChanged(this, PropertyChanged);
DependencyPropertyDescriptor.FromProperty(MaxValueProperty, typeof(NumericSpinner)).AddValueChanged(this, PropertyChanged);
PropertyChanged += (x, y) => validate();
}
#region ValueProperty
public readonly static DependencyProperty ValueProperty = DependencyProperty.Register(
"Value",
typeof(decimal),
typeof(NumericSpinner),
new PropertyMetadata(new decimal(0)));
public decimal Value
{
get { return (decimal)GetValue(ValueProperty); }
set
{
if (value < MinValue)
value = MinValue;
if (value > MaxValue)
value = MaxValue;
SetValue(ValueProperty, value);
ValueChanged(this,new EventArgs());
}
}
#endregion
#region StepProperty
public readonly static DependencyProperty StepProperty = DependencyProperty.Register(
"Step",
typeof(decimal),
typeof(NumericSpinner),
new PropertyMetadata(new decimal(0.1)));
public decimal Step
{
get { return (decimal)GetValue(StepProperty); }
set
{
SetValue(StepProperty, value);
}
}
#endregion
#region DecimalsProperty
public readonly static DependencyProperty DecimalsProperty = DependencyProperty.Register(
"Decimals",
typeof(int),
typeof(NumericSpinner),
new PropertyMetadata(2));
public int Decimals
{
get { return (int)GetValue(DecimalsProperty); }
set
{
SetValue(DecimalsProperty, value);
}
}
#endregion
#region MinValueProperty
public readonly static DependencyProperty MinValueProperty = DependencyProperty.Register(
"MinValue",
typeof(decimal),
typeof(NumericSpinner),
new PropertyMetadata(decimal.MinValue));
public decimal MinValue
{
get { return (decimal)GetValue(MinValueProperty); }
set
{
if (value > MaxValue)
MaxValue = value;
SetValue(MinValueProperty, value);
}
}
#endregion
#region MaxValueProperty
public readonly static DependencyProperty MaxValueProperty = DependencyProperty.Register(
"MaxValue",
typeof(decimal),
typeof(NumericSpinner),
new PropertyMetadata(decimal.MaxValue));
public decimal MaxValue
{
get { return (decimal)GetValue(MaxValueProperty); }
set
{
if (value < MinValue)
value = MinValue;
SetValue(MaxValueProperty, value);
}
}
#endregion
/// <summary>
/// Revalidate the object, whenever a value is changed...
/// </summary>
private void validate()
{
// Logically, This is not needed at all... as it's handled within other properties...
if (MinValue > MaxValue) MinValue = MaxValue;
if (MaxValue < MinValue) MaxValue = MinValue;
if (Value < MinValue) Value = MinValue;
if (Value > MaxValue) Value = MaxValue;
Value = decimal.Round(Value, Decimals);
}
private void cmdUp_Click(object sender, RoutedEventArgs e)
{
Value += Step;
}
private void cmdDown_Click(object sender, RoutedEventArgs e)
{
Value -= Step;
}
private void tb_main_Loaded(object sender, RoutedEventArgs e)
{
ValueChanged(this, new EventArgs());
}
}
}