回答編集履歴
1
継承で実現する案を追記しました
test
CHANGED
@@ -175,3 +175,79 @@
|
|
175
175
|
}
|
176
176
|
|
177
177
|
```
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
---
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
※継承で実現したいとのことだったので、以下に別案を追記します。
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
TextChanged内で例外を投げるようにしておけば、
|
190
|
+
|
191
|
+
XAML編集欄を勝手に書き換えないまでも、エラーの下線は表示されるので事足りるのではないかと思います。
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
```C#
|
196
|
+
|
197
|
+
public class NumericTextBox : TextBox
|
198
|
+
|
199
|
+
{
|
200
|
+
|
201
|
+
static NumericTextBox()
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
TextProperty.OverrideMetadata(typeof(NumericTextBox), new FrameworkPropertyMetadata("0", onTextChanged));
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
private static void onTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
|
212
|
+
|
213
|
+
{
|
214
|
+
|
215
|
+
var input = e.NewValue as string;
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
decimal _;
|
220
|
+
|
221
|
+
if (!decimal.TryParse(input, out _))
|
222
|
+
|
223
|
+
{
|
224
|
+
|
225
|
+
MessageBox.Show("ダメです");
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
if (DesignerProperties.GetIsInDesignMode(sender))
|
230
|
+
|
231
|
+
{
|
232
|
+
|
233
|
+
//デザイナ上では例外をなげる
|
234
|
+
|
235
|
+
throw new ArgumentException("ダメです");
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
//実行中は0に戻す
|
242
|
+
|
243
|
+
(sender as NumericTextBox).Text = "0";
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
|