-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleUpDownBehaviors.cs
122 lines (109 loc) · 4.49 KB
/
DoubleUpDownBehaviors.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
using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Xceed.Wpf.Toolkit;
namespace NumericalUpDownSample
{
class DoubleUpDownBehaviors
{
/// <summary>
/// True なら入力を数字のみに制限します。
/// </summary>
public static readonly DependencyProperty IsNumericProperty =
DependencyProperty.RegisterAttached(
"IsNumeric", typeof(bool),
typeof(DoubleUpDownBehaviors),
new UIPropertyMetadata(false, IsNumericChanged)
);
[AttachedPropertyBrowsableForType(typeof(DoubleUpDown))]
public static bool GetIsNumeric(DependencyObject obj)
{
return (bool)obj.GetValue(IsNumericProperty);
}
[AttachedPropertyBrowsableForType(typeof(DoubleUpDownBehaviors))]
public static void SetIsNumeric(DependencyObject obj, bool value)
{
obj.SetValue(IsNumericProperty, value);
}
private static void IsNumericChanged
(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var doubleUpDown = sender as DoubleUpDown;
if (doubleUpDown == null) return;
// イベントを登録・削除
doubleUpDown.PreviewTextInput -= DoubleUpDown_PreviewTextInput;
DataObject.RemovePastingHandler(doubleUpDown, textbox_PastingHandler);
doubleUpDown.Loaded -= DoubleUpDown_Loaded;
var isNumeric = (bool)e.NewValue;
if (isNumeric)
{
doubleUpDown.Loaded += DoubleUpDown_Loaded;
doubleUpDown.PreviewTextInput += DoubleUpDown_PreviewTextInput;
DataObject.AddPastingHandler(doubleUpDown, textbox_PastingHandler);
}
}
private static void DoubleUpDown_Loaded(object sender, RoutedEventArgs e)
{
var doubleUpDown = sender as DoubleUpDown;
var textBox = doubleUpDown.Template.FindName("PART_TextBox",doubleUpDown) as WatermarkTextBox;
if (null != textBox)
{
InputMethod.SetIsInputMethodEnabled(textBox, false);
}
}
//途中段階で、どこまで許可するか
//入力中は緩めにしたいが、小数点とかはひとつにしたい。
//ただ、ロケールにより、,と.両方を許可する必要あり
//TryParseだと途中経過が、.だけに何故かなるので難しい
private static void DoubleUpDown_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
if (!DoubleValidationRule.IsCanInputKey(e.Text))
{
e.Handled = true;
return;
}
var doubleUpDown = sender as DoubleUpDown;
var textBox = doubleUpDown.Template.FindName("PART_TextBox",doubleUpDown) as WatermarkTextBox;
if (textBox == null)
{
return;
}
var changedText =
textBox.Text
.Remove(textBox.SelectionStart, textBox.SelectionLength)
.Insert(textBox.SelectionStart, e.Text)
;
if (!DoubleValidationRule.IsCanInputString(changedText))
{
e.Handled = true;
}
}
//ペーストに対応するために必要
private static void textbox_PastingHandler(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(DataFormats.Text))
{
string pasted_text = Convert.ToString(e.DataObject.GetData(DataFormats.Text));
if (pasted_text == null)
{
e.CancelCommand();
}
var doubleUpDown = sender as DoubleUpDown;
var textBox = doubleUpDown.Template.FindName("PART_TextBox",doubleUpDown) as WatermarkTextBox;
var changedText =
textBox.Text
.Remove(textBox.SelectionStart, textBox.SelectionLength)
.Insert(textBox.SelectionStart, pasted_text);
if (!DoubleValidationRule.IsCanInputString(changedText))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
}
}