回答編集履歴

1

追記

2018/09/19 15:20

投稿

Zuishin
Zuishin

スコア28660

test CHANGED
@@ -5,3 +5,67 @@
5
5
  検証の方法が色々載っています。
6
6
 
7
7
  個々の値が検証されれば全体検証は必要ないんじゃないかと思いますが、例えばすべての入力について問題ない時だけボタンの Enabled が true になるようにすればいいのではないでしょうか。
8
+
9
+
10
+
11
+ #追記
12
+
13
+
14
+
15
+ そういえば ReactiveProperty でしたね。
16
+
17
+ ではこういうのはどうでしょう?
18
+
19
+ 次の AddIntegers を ViewModel とし、First, Second, Answer をそれぞれテキストボックスにバインドします。
20
+
21
+ 最初 CorrectAnswer.Value は null なので、エラーは起きません。
22
+
23
+ ボタンをクリックすることで CorrectAnswer.Value を First.Value + Second.Value に設定します。
24
+
25
+ そうした場合、入力が間違っていれば Answer が間違っていることになり、赤枠が付きます。
26
+
27
+
28
+
29
+ ```C#
30
+
31
+ public class AddIntegers
32
+
33
+ {
34
+
35
+ public AddIntegers()
36
+
37
+ {
38
+
39
+ Answer = new ReactiveProperty<int>(0)
40
+
41
+ .SetValidateNotifyError(a =>
42
+
43
+ a == CorrectAnswer.Value || CorrectAnswer.Value == null
44
+
45
+ ? null
46
+
47
+ : "間違っています");
48
+
49
+ }
50
+
51
+
52
+
53
+ public ReactiveProperty<int> First { get; } = new ReactiveProperty<int>();
54
+
55
+ public ReactiveProperty<int> Second { get; } = new ReactiveProperty<int>();
56
+
57
+ public ReactiveProperty<int> Answer { get; private set; }
58
+
59
+ protected ReactiveProperty<int?> CorrectAnswer { get; } = new ReactiveProperty<int?>();
60
+
61
+ ```
62
+
63
+
64
+
65
+ この場合は「入力」と「あるべき値」と比較して検証していますが、あるべき値が決まっていなくても同様に true/false で条件付けられるものなら何でも使えます。
66
+
67
+ つまり、入力の一つ一つに対して、それが正しいか間違っているかを表すプロパティを作るということです。
68
+
69
+ プロパティの数は増えますが、計算の負荷が高い以上致し方ないものと思います。
70
+
71
+ キャッシュと考えてください。