回答編集履歴

2

見直しキャンペーン中

2023/07/21 09:24

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,229 +1,115 @@
1
1
  `Younger()`を実行したときに`Age`が変わるのは、作った人にしかわからないのでどうにかして知らせる必要があります。
2
2
 
3
-
4
-
5
3
  `INotifyPropertyChanged`がそれです。
6
-
7
4
  `ToReactivePropertyAsSynchronized`は`INotifyPropertyChanged`の変更通知を購読し、反映する仕組みですが`Person`側で通知をしていません。
8
-
9
5
  通知をしてください(Age2)
10
-
11
-
12
6
 
13
7
  `ReactivePropertySlim`を使ってもよいでしょう(Age3)
14
8
 
15
-
16
-
17
9
  `Age { get; set; }`を維持するならViewModel側で変えることになりますが、筋はよくなさそうです(Age1)
18
10
 
19
-
20
-
21
- ```xaml
11
+ ```xml
22
-
23
12
  <Window
24
-
25
13
  x:Class="Questions250717.MainWindow"
26
-
27
14
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
28
-
29
15
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
30
-
31
16
  Width="800"
32
-
33
17
  Height="450">
34
-
35
18
  <StackPanel>
36
-
37
19
  <HeaderedContentControl Header="Age1">
38
-
39
20
  <TextBox Text="{Binding Age1.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
40
-
41
21
  </HeaderedContentControl>
42
-
43
22
  <HeaderedContentControl Header="Age2">
44
-
45
23
  <TextBox Text="{Binding Age2.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
46
-
47
24
  </HeaderedContentControl>
48
-
49
25
  <HeaderedContentControl Header="Age3">
50
-
51
26
  <TextBox Text="{Binding Age3.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
52
-
53
27
  </HeaderedContentControl>
54
-
55
28
  <Button Command="{Binding YoungerCommand}">
56
-
57
29
  <TextBlock Text="若返る" />
58
-
59
30
  </Button>
60
-
61
31
  <Button Command="{Binding ElderCommand}">
62
-
63
32
  <TextBlock Text="老いる" />
64
-
65
33
  </Button>
66
-
67
34
  </StackPanel>
68
-
69
35
  </Window>
70
-
71
36
  ```
72
37
 
73
-
74
-
75
- ```C#
38
+ ```cs
76
-
77
39
  using System.Windows;
78
-
79
40
  using Prism.Mvvm;
80
-
81
41
  using Reactive.Bindings;
82
-
83
42
  using Reactive.Bindings.Extensions;
84
43
 
85
-
86
-
87
44
  namespace Questions250717
88
-
89
45
  {
90
-
91
46
  public class Person : BindableBase
92
-
93
47
  {
94
-
95
48
  public int Age1 { get; set; }
96
49
 
97
-
98
-
99
50
  public int Age2 { get => _Age2; set => SetProperty(ref _Age2, value); }
100
-
101
51
  private int _Age2;
102
-
103
-
104
52
 
105
53
  public ReactivePropertySlim<int> Age3 { get; }
106
54
 
107
-
108
-
109
55
  public Person()
110
-
111
56
  {
112
-
113
57
  Age1 = 55;
114
-
115
58
  Age2 = 55;
116
-
117
59
  Age3 = new ReactivePropertySlim<int>(55);
118
-
119
60
  }
120
61
 
121
-
122
-
123
62
  public void Younger()
124
-
125
63
  {
126
-
127
64
  //Age1--;
128
-
129
65
  Age2--;
130
-
131
66
  Age3.Value--;
132
-
133
67
  }
134
68
 
135
-
136
-
137
69
  public void Elder()
138
-
139
70
  {
140
-
141
71
  //Age1++;
142
-
143
72
  Age2++;
144
-
145
73
  Age3.Value++;
146
-
147
74
  }
148
-
149
75
  }
150
-
151
76
  public class PersonViewModel : BindableBase
152
-
153
77
  {
154
-
155
78
  public ReactiveProperty<int> Age1 { get; }
156
-
157
79
  public ReactiveProperty<int> Age2 { get; }
158
-
159
80
  public ReactiveProperty<int> Age3 { get; }
160
81
 
161
-
162
-
163
82
  public ReactiveCommand YoungerCommand { get; }
164
-
165
83
  public ReactiveCommand ElderCommand { get; }
166
-
167
-
168
84
 
169
85
  private readonly Person person;
170
86
 
171
-
172
-
173
87
  public PersonViewModel(Person person)
174
-
175
88
  {
176
-
177
89
  this.person = person;
178
-
179
90
  Age1 = ReactiveProperty.FromObject(this.person, x => x.Age1);
180
-
181
91
  Age2 = this.person.ToReactivePropertyAsSynchronized(x => x.Age2);
182
-
183
92
  Age3 = this.person.Age3.ToReactiveProperty();
184
93
 
185
-
186
-
187
94
  YoungerCommand = new ReactiveCommand().WithSubscribe(() =>
188
-
189
95
  {
190
-
191
96
  Age1.Value--;
192
-
193
97
  this.person.Younger();
194
-
195
98
  });
196
-
197
99
  ElderCommand = new ReactiveCommand().WithSubscribe(() =>
198
-
199
100
  {
200
-
201
101
  Age1.Value++;
202
-
203
102
  this.person.Elder();
204
-
205
103
  });
206
-
207
104
  }
208
-
209
105
  }
210
-
211
106
  public partial class MainWindow : Window
212
-
213
107
  {
214
-
215
108
  public MainWindow()
216
-
217
109
  {
218
-
219
110
  InitializeComponent();
220
-
221
111
  DataContext = new PersonViewModel(new Person());
222
-
223
112
  }
224
-
225
113
  }
226
-
227
114
  }
228
-
229
115
  ```

1

Age1 { get; set; }

2020/04/01 09:36

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- ModelいじりたくければViewModel側で変えることになりますが、筋はよくなさそうです(Age1)
17
+ `Age { get; set; }`維持するViewModel側で変えることになりますが、筋はよくなさそうです(Age1)
18
18
 
19
19
 
20
20