-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathNumericUpDownWindow.xaml.cs
252 lines (216 loc) · 9.13 KB
/
NumericUpDownWindow.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
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
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 WPFNumericUpDown;
namespace GHCustomControls
{
/// <summary>
/// Interaction logic for TextBoxWindow.xaml
/// </summary>
public partial class NumericUpDownWindow : Window
{
Point _location;
float _scale;
public NumericUpDownWindow(Point location)
{
InitializeComponent();
_location = location;
//_scale = size;
}
private void MoveBottomRightEdgeOfWindowToMousePosition()
{
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
//var mouse = transform.Transform(GetMousePosition());
var pos = transform.Transform(_location);
//var pos = _location;
//this.Width *= _scale;
//this.Height *= _scale;
Left = pos.X - ActualWidth / 2.0;
Top = pos.Y - ActualHeight;
}
public System.Windows.Point GetMousePosition()
{
System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
return new System.Windows.Point(point.X, point.Y);
}
internal void SetInteger(IntegerNumber number)
{
IntegerNumericControl control = new IntegerNumericControl()
{
FontSize = 14,
Minimum = number.Min,
Maximum = number.Max,
Inc = number.Inc,
Decimals = number.Decimals
};
Binding valueBinding = new Binding("Value") { Mode = BindingMode.TwoWay };
valueBinding.Source = number;
BindingOperations.SetBinding(control, IntegerNumericControl.DATAProperty, valueBinding);
control.KeyDown += Control_KeyDown;
this.PlaceHolder.Children.Add(control);
}
internal void SetFloat(DecimalNumber number)
{
DecimalNumericControl control = new DecimalNumericControl()
{
FontSize = 14,
Minimum = number.Min,
Maximum = number.Max,
Inc = number.Inc,
Decimals = number.Decimals
};
Binding valueBinding = new Binding("Value") { Mode = BindingMode.TwoWay };
valueBinding.Source = number;
BindingOperations.SetBinding(control, DecimalNumericControl.DATAProperty, valueBinding);
control.KeyDown += Control_KeyDown;
this.PlaceHolder.Children.Add(control);
}
internal void SetDouble(DoubleNumber number)
{
DoubleNumericControl control = new DoubleNumericControl()
{
FontSize = 14,
Minimum = number.Min,
Maximum = number.Max,
Inc = number.Inc,
Decimals = number.Decimals
};
Binding valueBinding = new Binding("Value") { Mode = BindingMode.TwoWay };
valueBinding.Source =number;
BindingOperations.SetBinding(control, DoubleNumericControl.DATAProperty, valueBinding);
control.KeyDown += Control_KeyDown;
this.PlaceHolder.Children.Add(control);
}
internal void SetGeneric<T>(GenericNumber<T> number) where T : struct, IFormattable, IComparable<T>
{
GenericNumericControl<T> control = new GenericNumericControl<T>()
{
FontSize = 14,
Minimum = number.Min,
Maximum = number.Max,
Inc = number.Inc,
Decimals = number.Decimals
};
Binding valueBinding = new Binding("Value") { Mode = BindingMode.TwoWay };
valueBinding.Source = number;
BindingOperations.SetBinding(control, GenericNumericControl<T>.DATAProperty, valueBinding);
control.KeyDown += Control_KeyDown;
control.Background = Brushes.LightYellow;
this.PlaceHolder.Children.Add(control);
}
//internal void SetData<T>(GenericNumber<T> number) where T:struct, IFormattable, IComparable<T>
//{
// Control control;
// //Binding binding = new Binding("Value");
// //binding.Source = numericUpDownControl;
// //if (typeof(T) == typeof(int))
// //{
// // var numeric = new IntegerUpDown();
// // numeric.Minimum = Convert.ToInt32(numericUpDownControl._min);
// // numeric.Maximum = Convert.ToInt32(numericUpDownControl._max);
// // numeric.UpdateValueOnEnterKey = true;
// // numeric.FormatString = numericUpDownControl.FormatString;
// // numeric.SetBinding(IntegerUpDown.ValueProperty, binding);
// // control = numeric;
// //}
// //else if (typeof(T) == typeof(decimal))
// //{
// // var numeric = new DecimalUpDown();
// // numeric.Minimum = Convert.ToDecimal(numericUpDownControl._min);
// // numeric.Maximum = Convert.ToDecimal(numericUpDownControl._max);
// // numeric.Increment = (numeric.Maximum - numeric.Minimum) / 100;
// // numeric.UpdateValueOnEnterKey = true;
// // numeric.FormatString = numericUpDownControl.FormatString;
// // numeric.SetBinding(DecimalUpDown.ValueProperty, binding);
// // control = numeric;
// //}
// //else if (typeof(T) == typeof(double))
// //{
// // var numeric = new DoubleUpDown();
// // numeric.Minimum = Convert.ToDouble(numericUpDownControl._min);
// // numeric.Maximum = Convert.ToDouble(numericUpDownControl._max);
// // numeric.UpdateValueOnEnterKey = true;
// // numeric.SetBinding(DoubleUpDown.ValueProperty, binding);
// // numeric.FormatString = numericUpDownControl.FormatString;
// // control = numeric;
// //}
// //else
// //{
// // throw new Exception($"Inavlid type, Expected int,double or decimal received {typeof(T)}");
// //}
// //control.Width = 200;
// //control.Height = 24;
// // control.Name = "Numeric";
// //control.Background = Brushes.LightGoldenrodYellow;
// //control.KeyDown += Control_KeyDown;
// //this.PlaceHolder.Children.Add(control);
//}
private void Control_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
DialogResult = true;
this.Close();
}
}
//object _value;
//public object Value
//{
// get => _value;
// set
// {
// _value = value;
// if (_value is int)
// {
// (control as IntegerUpDown).Value = (int)_value;
// }else if (_value is decimal)
// {
// (control as DecimalUpDown).Value = (decimal)_value;
// }
// else if (_value is double)
// {
// (control as DoubleUpDown).Value = (double)_value;
// }
// }
//}
private void Button_Click(object sender, RoutedEventArgs e)
{
//_value = (int)this.control.Value;
DialogResult = true;
this.Close();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MoveBottomRightEdgeOfWindowToMousePosition();
}
private void Window_MouseLeave(object sender, MouseEventArgs e)
{
if (sender != null)
{
Window w = sender as Window;
if (w.IsVisible)
w.Close();
}
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape )
{
Window w = sender as Window;
if (w.IsVisible)
w.Close();
}
}
}
}