回答編集履歴

1

コード例

2016/09/23 00:21

投稿

ozwk
ozwk

スコア13528

test CHANGED
@@ -1,3 +1,121 @@
1
1
  プロパティを10個用意するのではなく
2
2
 
3
3
  1つのプロパティを持ったVMのインスタンスを10個用意します。
4
+
5
+
6
+
7
+ ---
8
+
9
+
10
+
11
+ こんな感じです。
12
+
13
+ (ReactiveCommand使いましたが、本題とは関係ないので気にしないでください。)
14
+
15
+
16
+
17
+
18
+
19
+ ```xml
20
+
21
+ <StackPanel>
22
+
23
+ <Button Content="Click!" Command="{Binding OkButtonCommand}"/>
24
+
25
+ <TextBlock Text="{Binding PropArray[0].Value}"/>
26
+
27
+ <TextBlock Text="{Binding PropArray[1].Value}"/>
28
+
29
+ </StackPanel>
30
+
31
+ ```
32
+
33
+ ```C#
34
+
35
+ class MainVM
36
+
37
+ {
38
+
39
+ public SomeVM[] PropArray { get; } = new SomeVM[] { new SomeVM(1), new SomeVM(2) };
40
+
41
+ public ReactiveCommand OkButtonCommand { get; }
42
+
43
+ public MainVM()
44
+
45
+ {
46
+
47
+ OkButtonCommand = new ReactiveCommand();
48
+
49
+ OkButtonCommand.Subscribe(_ => SomethingToDo());
50
+
51
+ }
52
+
53
+
54
+
55
+ private void SomethingToDo()
56
+
57
+ {
58
+
59
+ PropArray[0].Value++;
60
+
61
+ }
62
+
63
+ }
64
+
65
+ ```
66
+
67
+ ```C#
68
+
69
+ class SomeVM : INotifyPropertyChanged
70
+
71
+ {
72
+
73
+ public event PropertyChangedEventHandler PropertyChanged;
74
+
75
+ protected void OnPropertyChanged(PropertyChangedEventArgs e)
76
+
77
+ {
78
+
79
+ PropertyChanged?.Invoke(this, e);
80
+
81
+ }
82
+
83
+
84
+
85
+ private int _value;
86
+
87
+ public int Value
88
+
89
+ {
90
+
91
+ get { return _value; }
92
+
93
+ set
94
+
95
+ {
96
+
97
+ if (value == _value)
98
+
99
+ return;
100
+
101
+ _value = value;
102
+
103
+ OnPropertyChanged(new PropertyChangedEventArgs(nameof(Value)));
104
+
105
+ }
106
+
107
+ }
108
+
109
+
110
+
111
+ public SomeVM(int name)
112
+
113
+ {
114
+
115
+ _value = name;
116
+
117
+ }
118
+
119
+ }
120
+
121
+ ```