-
Notifications
You must be signed in to change notification settings - Fork 494
/
NumericUpDown.vb
369 lines (289 loc) · 10.9 KB
/
NumericUpDown.vb
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
'<SnippetControlLogic>
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Controls.Primitives
Imports System.Windows.Input
'<SnippetClassAttributes>
<TemplatePart(Name:="TextElement", Type:=GetType(TextBlock))> _
<TemplatePart(Name:="UpButtonElement", Type:=GetType(RepeatButton))> _
<TemplatePart(Name:="DownButtonElement", Type:=GetType(RepeatButton))> _
<TemplateVisualState(Name:="Positive", GroupName:="ValueStates")> _
<TemplateVisualState(Name:="Negative", GroupName:="ValueStates")> _
<TemplateVisualState(Name:="Focused", GroupName:="FocusedStates")> _
<TemplateVisualState(Name:="Unfocused", GroupName:="FocusedStates")> _
Public Class NumericUpDown
Inherits Control
'</SnippetClassAttributes>
Public Sub New()
DefaultStyleKey = GetType(NumericUpDown)
Me.IsTabStop = True
End Sub
Public Shared ReadOnly ValueProperty As DependencyProperty = _
DependencyProperty.Register("Value", GetType(Integer), _
GetType(NumericUpDown), _
New PropertyMetadata(0, New PropertyChangedCallback(AddressOf ValueChangedCallback)))
'<SnippetEntireValueChangedCallback>
'<SnippetValueChangedCallback>
Private Shared Sub ValueChangedCallback(ByVal obj As DependencyObject, _
ByVal args As DependencyPropertyChangedEventArgs)
Dim ctl As NumericUpDown = DirectCast(obj, NumericUpDown)
Dim newValue As Integer = args.NewValue
' Update the TextElement to the new value.
If ctl.TextElement IsNot Nothing Then
ctl.TextElement.Text = newValue.ToString()
End If
'</SnippetValueChangedCallback>
' Call UpdateStates because the Value might have caused the
' control to change ValueStates.
ctl.UpdateStates(True)
' Raise the ValueChanged event so applications can be alerted
' when Value changes.
Dim e As New ValueChangedEventArgs(newValue)
ctl.OnValueChanged(e)
'<SnippetValueChangedCallbackEnd>
End Sub
'</SnippetValueChangedCallbackEnd>
'</SnippetEntireValueChangedCallback>
Public Event ValueChanged As ValueChangedEventHandler
Protected Overridable Sub OnValueChanged(ByVal e As ValueChangedEventArgs)
RaiseEvent ValueChanged(Me, e)
End Sub
'<SnippetUpdateStates>
Private Sub UpdateStates(ByVal useTransitions As Boolean)
'<SnippetValueStateChange>
If Value >= 0 Then
VisualStateManager.GoToState(Me, "Positive", useTransitions)
Else
VisualStateManager.GoToState(Me, "Negative", useTransitions)
End If
'</SnippetValueStateChange>
If isFocused Then
VisualStateManager.GoToState(Me, "Focused", useTransitions)
Else
VisualStateManager.GoToState(Me, "Unfocused", useTransitions)
End If
End Sub
'</SnippetUpdateStates>
Public Property Value() As Integer
Get
Return CInt(GetValue(ValueProperty))
End Get
Set(ByVal value As Integer)
SetValue(ValueProperty, value)
End Set
End Property
'<SnippetApplyTemplate>
Protected Overloads Sub OnApplyTemplate()
UpButtonElement = TryCast(GetTemplateChild("UpButton"), RepeatButton)
DownButtonElement = TryCast(GetTemplateChild("DownButton"), RepeatButton)
TextElement = TryCast(GetTemplateChild("TextBlock"), TextBlock)
UpdateStates(False)
End Sub
'</SnippetApplyTemplate>
Private m_textElement As TextBlock
Private Property TextElement() As TextBlock
Get
Return m_textElement
End Get
Set(ByVal txtBlock As TextBlock)
m_textElement = txtBlock
If m_textElement IsNot Nothing Then
m_textElement.Text = Value.ToString()
m_textElement.IsHitTestVisible = True
End If
End Set
End Property
Private m_downButtonElement As RepeatButton
Private Property DownButtonElement() As RepeatButton
Get
Return m_downButtonElement
End Get
Set(ByVal repeatBtn As RepeatButton)
If m_downButtonElement IsNot Nothing Then
RemoveHandler m_downButtonElement.Click, AddressOf downButtonElement_Click
End If
m_downButtonElement = repeatBtn
If m_downButtonElement IsNot Nothing Then
AddHandler m_downButtonElement.Click, AddressOf downButtonElement_Click
End If
End Set
End Property
Private Sub downButtonElement_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Value -= 1
End Sub
'<SnippetUpButtonProperty>
Private m_upButtonElement As RepeatButton
Private Property UpButtonElement() As RepeatButton
Get
Return m_upButtonElement
End Get
Set(ByVal repeatBtn As RepeatButton)
If m_upButtonElement IsNot Nothing Then
RemoveHandler m_upButtonElement.Click, AddressOf upButtonElement_Click
End If
m_upButtonElement = repeatBtn
If m_upButtonElement IsNot Nothing Then
AddHandler m_upButtonElement.Click, AddressOf upButtonElement_Click
End If
End Set
End Property
'</SnippetUpButtonProperty>
Private Sub upButtonElement_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Value += 1
End Sub
'<SnippetControlFocus>
Protected Overloads Overrides Sub OnPointerPressed(ByVal e As PointerRoutedEventArgs)
MyBase.OnPointerPressed(e)
Focus()
End Sub
'</SnippetControlFocus>
'<SnippetFocusProperty>
Private isFocused As Boolean
'<SnippetOnGotFocus>
Protected Overloads Overrides Sub OnGotFocus(ByVal e As RoutedEventArgs)
MyBase.OnGotFocus(e)
isFocused = True
UpdateStates(True)
End Sub
'</SnippetOnGotFocus>
Protected Overloads Overrides Sub OnLostFocus(ByVal e As RoutedEventArgs)
MyBase.OnLostFocus(e)
isFocused = False
UpdateStates(True)
End Sub
'</SnippetFocusProperty>
End Class
Public Delegate Sub ValueChangedEventHandler(ByVal sender As Object, ByVal e As ValueChangedEventArgs)
Public Class ValueChangedEventArgs
Inherits EventArgs
Private _value As Integer
Public Sub New(ByVal num As Integer)
_value = num
End Sub
Public ReadOnly Property Value() As Integer
Get
Return _value
End Get
End Property
End Class
'</SnippetControlLogic>
Namespace ControlContract
'<SnippetControlContract>
<TemplatePart(Name:="TextElement", Type:=GetType(TextBlock))> _
<TemplatePart(Name:="UpButtonElement", Type:=GetType(RepeatButton))> _
<TemplatePart(Name:="DownButtonElement", Type:=GetType(RepeatButton))> _
<TemplateVisualState(Name:="Positive", GroupName:="ValueStates")> _
<TemplateVisualState(Name:="Negative", GroupName:="ValueStates")> _
<TemplateVisualState(Name:="Focused", GroupName:="FocusedStates")> _
<TemplateVisualState(Name:="Unfocused", GroupName:="FocusedStates")> _
Public Class NumericUpDown
Inherits Control
Public Shared ReadOnly BackgroundProperty As DependencyProperty
Public Shared ReadOnly BorderBrushProperty As DependencyProperty
Public Shared ReadOnly BorderThicknessProperty As DependencyProperty
Public Shared ReadOnly FontFamilyProperty As DependencyProperty
Public Shared ReadOnly FontSizeProperty As DependencyProperty
Public Shared ReadOnly FontStretchProperty As DependencyProperty
Public Shared ReadOnly FontStyleProperty As DependencyProperty
Public Shared ReadOnly FontWeightProperty As DependencyProperty
Public Shared ReadOnly ForegroundProperty As DependencyProperty
Public Shared ReadOnly HorizontalContentAlignmentProperty As DependencyProperty
Public Shared ReadOnly PaddingProperty As DependencyProperty
Public Shared ReadOnly TextAlignmentProperty As DependencyProperty
Public Shared ReadOnly TextDecorationsProperty As DependencyProperty
Public Shared ReadOnly TextWrappingProperty As DependencyProperty
Public Shared ReadOnly VerticalContentAlignmentProperty As DependencyProperty
Public Property Background() As Brush
Get
End Get
Set(ByVal value As Brush)
End Set
End Property
Public Property BorderBrush() As Brush
Get
End Get
Set(ByVal value As Brush)
End Set
End Property
Public Property BorderThickness() As Thickness
Get
End Get
Set(ByVal value As Thickness)
End Set
End Property
Public Property FontFamily() As FontFamily
Get
End Get
Set(ByVal value As FontFamily)
End Set
End Property
Public Property FontSize() As Double
Get
End Get
Set(ByVal value As Double)
End Set
End Property
Public Property FontStretch() As FontStretch
Get
End Get
Set(ByVal value As FontStretch)
End Set
End Property
Public Property FontStyle() As FontStyle
Get
End Get
Set(ByVal value As FontStyle)
End Set
End Property
Public Property FontWeight() As FontWeight
Get
End Get
Set(ByVal value As FontWeight)
End Set
End Property
Public Property Foreground() As Brush
Get
End Get
Set(ByVal value As Brush)
End Set
End Property
Public Property HorizontalContentAlignment() As HorizontalAlignment
Get
End Get
Set(ByVal value As HorizontalAlignment)
End Set
End Property
Public Property Padding() As Thickness
Get
End Get
Set(ByVal value As Thickness)
End Set
End Property
Public Property TextAlignment() As TextAlignment
Get
End Get
Set(ByVal value As TextAlignment)
End Set
End Property
Public Property TextDecorations() As TextDecorationCollection
Get
End Get
Set(ByVal value As TextDecorationCollection)
End Set
End Property
Public Property TextWrapping() As TextWrapping
Get
End Get
Set(ByVal value As TextWrapping)
End Set
End Property
Public Property VerticalContentAlignment() As VerticalAlignment
Get
End Get
Set(ByVal value As VerticalAlignment)
End Set
End Property
End Class
'</SnippetControlContract>
End Namespace