-
Notifications
You must be signed in to change notification settings - Fork 3
/
IntegerTextBox.cs
176 lines (162 loc) · 8.58 KB
/
IntegerTextBox.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
/*
MIT License
Copyright(c) 2019 Dennis Geller
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Data;
namespace FancyPrimitives
{
//**************************************************************************************************************************************************
public class IntegerTextBox : TextBox
{
private static readonly Regex regex = new Regex("^[-]?[0-9]+$");
private int initialValue;
//----------------------------------------------------------------------------------------------------------------------------------
public IntegerTextBox()
{
PreviewKeyDown += OnPreviewKeyDown;
AddHandler(System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent,
new System.Windows.Controls.TextChangedEventHandler(OnTextChanged));
}
//----------------------------------------------------------------------------------------------------------------------------------
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs args)
{
initialValue = Convert.ToInt32(Text);
base.OnGotKeyboardFocus(args);
}
//----------------------------------------------------------------------------------------------------------------------------------
protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs args)
{
if (Text.Trim() == "")
Text = "0";
base.OnLostKeyboardFocus(args);
}
//----------------------------------------------------------------------------------------------------------------------------------
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
string new_str = Text.Substring(0, SelectionStart) + e.Text + Text.Substring(SelectionStart + SelectionLength, Text.Length - SelectionStart - SelectionLength);
if (!regex.IsMatch(new_str))
e.Handled = true;
int.TryParse(new_str, out int newIntValue);
if (newIntValue < MinValue || newIntValue > MaxValue)
e.Handled = true;
base.OnPreviewTextInput(e);
}
//----------------------------------------------------------------------------------------------------------------------------------
protected void OnPreviewKeyDown(object sender, KeyEventArgs args)
{
IntegerTextBox txtBox = sender as IntegerTextBox;
if (args.Key == Key.Escape)
{
args.Handled = true;
txtBox.Text = initialValue.ToString();
Keyboard.ClearFocus();
}
else if (args.Key == Key.Enter)
{
args.Handled = true;
//txtBox.Text = lastValidValue.ToString();
Keyboard.ClearFocus();
}
}
//----------------------------------------------------------------------------------------------------------------------------------
void OnTextChanged(object sender, TextChangedEventArgs e)
{
IntegerTextBox txtBox = sender as IntegerTextBox;
if (Text.Trim() == "") return;
if (Command != null)
{
BindingExpressionBase expr = BindingOperations.GetBindingExpressionBase(txtBox, CommandParameterProperty);
if (expr != null)
expr.UpdateTarget();
//Debug.WriteLine($"Send command ({d})");
if (Command != null && Command.CanExecute(CommandParameter))
Command.Execute(CommandParameter);
}
}
//----------------------------------------------------------------------------------------------------------------------------------
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(IntegerTextBox), new PropertyMetadata(null));
//----------------------------------------------------------------------------------------------------------------------------------
public Object CommandParameter
{
get { return (Object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(Object), typeof(IntegerTextBox), new PropertyMetadata(null));
//----------------------------------------------------------------------------------------------------------------------------------
public int MinValue
{
get { return (int)GetValue(MinValueProperty); }
set { SetValue(MinValueProperty, value); }
}
public static readonly DependencyProperty MinValueProperty =
DependencyProperty.Register("MinValue", typeof(int), typeof(IntegerTextBox), new PropertyMetadata(int.MinValue));
//----------------------------------------------------------------------------------------------------------------------------------
public int MaxValue
{
get { return (int)GetValue(MaxValueProperty); }
set { SetValue(MaxValueProperty, value); }
}
public static readonly DependencyProperty MaxValueProperty =
DependencyProperty.Register("MaxValue", typeof(int), typeof(IntegerTextBox), new PropertyMetadata(int.MaxValue));
//----------------------------------------------------------------------------------------------------------------------------------
private RelayCommand incrementValueCommand;
public RelayCommand IncrementValueCommand
{
get
{
return incrementValueCommand ??
(incrementValueCommand = new RelayCommand(obj =>
{
int.TryParse(Text, out int intValue);
if (intValue < MaxValue)
SetCurrentValue(TextProperty, (intValue + 1).ToString());
}));
}
}
//----------------------------------------------------------------------------------------------------------------------------------
private RelayCommand decrementValueCommand;
public RelayCommand DecrementValueCommand
{
get
{
return decrementValueCommand ??
(decrementValueCommand = new RelayCommand(obj =>
{
int.TryParse(Text, out int intValue);
if (intValue > MinValue)
SetCurrentValue(TextProperty, (intValue - 1).ToString());
}));
}
}
//----------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------
}
}