回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,419 +1,210 @@
|
|
1
1
|
申し訳ないですけど、参考サイトはだいぶひどいですね。。。
|
2
|
-
|
3
2
|
わかっている人には何の知見も得られませんし、勉強中の方には省略部分が多すぎて参考になりません(今どきは完全版をGitHubに上げてリンクしたりするものですが)
|
4
3
|
|
5
|
-
|
6
|
-
|
7
4
|
記事の意図のプロジェクト構成はこうです。
|
8
|
-
|
9
5
|
![ソリューションエクスプローラー](e326bddd491930ced7b11d200325c7c7.png)
|
10
6
|
|
11
|
-
|
12
|
-
|
13
7
|
`ViewModels`・`Views`フォルダ以下に、それぞれの`ViewModel`・`View`があるという前提です。
|
14
|
-
|
15
8
|
そのフォルダ(正確には名前空間)を、xmlnsで`vm`・`view`と指定しています。
|
16
9
|
|
17
|
-
|
18
|
-
|
19
10
|
現状フォルダ分けされていないのと、それぞれの`View`がありません(通常`UserControl`です)
|
20
11
|
|
21
|
-
|
22
|
-
|
23
12
|
注)
|
24
|
-
|
25
13
|
`MainWindow`も`Views`の中に入れているのかもしれませんが、名前がややこしいのであえて入れていません。
|
26
|
-
|
27
14
|
`MainWindowViewModel`もあると思われますが、質問の本題でないので作りません。
|
28
15
|
|
29
|
-
|
30
|
-
|
31
16
|
---
|
32
|
-
|
33
17
|
### MainWindow
|
34
|
-
|
35
|
-
```x
|
18
|
+
```xml
|
36
|
-
|
37
19
|
<Window
|
38
|
-
|
39
20
|
x:Class="Questions374820.MainWindow"
|
40
|
-
|
41
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
21
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
42
|
-
|
43
22
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
44
|
-
|
45
23
|
xmlns:view="clr-namespace:Questions374820.Views"
|
46
|
-
|
47
24
|
xmlns:vm="clr-namespace:Questions374820.ViewModels"
|
48
|
-
|
49
25
|
Title="MainWindow"
|
50
|
-
|
51
26
|
Width="800"
|
52
|
-
|
53
27
|
Height="450">
|
54
|
-
|
55
28
|
<DockPanel>
|
56
|
-
|
57
29
|
<DockPanel.Resources>
|
58
|
-
|
59
30
|
<DataTemplate DataType="{x:Type vm:MainViewModel}">
|
60
|
-
|
61
31
|
<view:MainView />
|
62
|
-
|
63
32
|
</DataTemplate>
|
64
|
-
|
65
33
|
<DataTemplate DataType="{x:Type vm:Sub1ViewModel}">
|
66
|
-
|
67
34
|
<view:Sub1View />
|
68
|
-
|
69
35
|
</DataTemplate>
|
70
|
-
|
71
36
|
<DataTemplate DataType="{x:Type vm:Sub2ViewModel}">
|
72
|
-
|
73
37
|
<view:Sub2View />
|
74
|
-
|
75
38
|
</DataTemplate>
|
76
|
-
|
77
39
|
</DockPanel.Resources>
|
78
|
-
|
79
40
|
<UniformGrid DockPanel.Dock="Top" Rows="1">
|
80
|
-
|
81
41
|
<Button Click="Button_Click" Content="メイン" />
|
82
|
-
|
83
42
|
<Button Click="Button_Click" Content="サブ1" />
|
84
|
-
|
85
43
|
<Button Click="Button_Click" Content="サブ2" />
|
86
|
-
|
87
44
|
</UniformGrid>
|
88
|
-
|
89
45
|
<ContentControl Content="{Binding SampleViewModel}" Focusable="False" />
|
90
|
-
|
91
46
|
</DockPanel>
|
92
|
-
|
93
47
|
</Window>
|
94
|
-
|
95
|
-
```
|
48
|
+
```
|
96
|
-
|
97
|
-
```
|
49
|
+
```cs
|
98
|
-
|
99
50
|
using System.ComponentModel;
|
100
|
-
|
101
51
|
using System.Windows;
|
102
|
-
|
103
|
-
using System.Windows.Controls;
|
52
|
+
using System.Windows.Controls;
|
104
|
-
|
105
53
|
using Questions374820.ViewModels;
|
106
54
|
|
107
|
-
|
108
|
-
|
109
55
|
namespace Questions374820
|
110
|
-
|
111
|
-
{
|
56
|
+
{
|
112
|
-
|
113
57
|
public partial class MainWindow : Window, INotifyPropertyChanged
|
114
|
-
|
115
|
-
{
|
58
|
+
{
|
116
|
-
|
117
59
|
public ViewModelBase SampleViewModel { get; set; } = new MainViewModel();
|
118
60
|
|
119
|
-
|
120
|
-
|
121
61
|
public MainWindow()
|
122
|
-
|
123
62
|
{
|
124
|
-
|
125
63
|
InitializeComponent();
|
126
|
-
|
127
64
|
DataContext = this;
|
128
|
-
|
129
65
|
}
|
130
66
|
|
131
|
-
|
132
|
-
|
133
67
|
private void Button_Click(object sender, RoutedEventArgs e)
|
134
|
-
|
135
68
|
{
|
136
|
-
|
137
69
|
var 画面 = ((Button)sender).Content as string;
|
138
|
-
|
139
70
|
switch (画面)
|
140
|
-
|
141
71
|
{
|
142
|
-
|
143
72
|
case "メイン":
|
144
|
-
|
145
73
|
SampleViewModel = new MainViewModel();
|
146
|
-
|
147
74
|
break;
|
148
|
-
|
149
75
|
case "サブ1":
|
150
|
-
|
151
76
|
SampleViewModel = new Sub1ViewModel();
|
152
|
-
|
153
77
|
break;
|
154
|
-
|
155
78
|
case "サブ2":
|
156
|
-
|
157
79
|
SampleViewModel = new Sub2ViewModel();
|
158
|
-
|
159
80
|
break;
|
160
|
-
|
161
81
|
}
|
162
82
|
|
163
|
-
|
164
|
-
|
165
83
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SampleViewModel)));
|
166
|
-
|
167
84
|
}
|
168
85
|
|
169
|
-
|
170
|
-
|
171
86
|
public event PropertyChangedEventHandler PropertyChanged;
|
172
|
-
|
173
|
-
}
|
87
|
+
}
|
174
|
-
|
175
|
-
}
|
88
|
+
}
|
176
|
-
|
177
|
-
```
|
89
|
+
```
|
178
|
-
|
179
|
-
|
180
90
|
|
181
91
|
### MainView
|
182
|
-
|
183
|
-
```x
|
92
|
+
```xml
|
184
|
-
|
185
93
|
<UserControl
|
186
|
-
|
187
94
|
x:Class="Questions374820.Views.MainView"
|
188
|
-
|
189
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
95
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
190
|
-
|
191
96
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
192
|
-
|
193
97
|
<Grid>
|
194
|
-
|
195
98
|
<TextBlock
|
196
|
-
|
197
99
|
HorizontalAlignment="Center"
|
198
|
-
|
199
100
|
VerticalAlignment="Center"
|
200
|
-
|
201
101
|
FontSize="36"
|
202
|
-
|
203
102
|
Text="MainView" />
|
204
|
-
|
205
103
|
</Grid>
|
206
|
-
|
207
104
|
</UserControl>
|
208
|
-
|
209
|
-
```
|
105
|
+
```
|
210
|
-
|
211
|
-
```
|
106
|
+
```cs
|
212
|
-
|
213
|
-
using System.Windows.Controls;
|
107
|
+
using System.Windows.Controls;
|
214
|
-
|
215
|
-
|
216
108
|
|
217
109
|
namespace Questions374820.Views
|
218
|
-
|
219
|
-
{
|
110
|
+
{
|
220
|
-
|
221
111
|
public partial class MainView : UserControl
|
222
|
-
|
223
|
-
{
|
112
|
+
{
|
224
|
-
|
225
113
|
public MainView() => InitializeComponent();
|
226
|
-
|
227
|
-
}
|
114
|
+
}
|
228
|
-
|
229
|
-
}
|
115
|
+
}
|
230
|
-
|
231
|
-
```
|
116
|
+
```
|
232
|
-
|
233
|
-
```
|
117
|
+
```cs
|
234
|
-
|
235
|
-
namespace Questions374820.ViewModels
|
118
|
+
namespace Questions374820.ViewModels
|
236
|
-
|
237
|
-
{
|
119
|
+
{
|
238
|
-
|
239
120
|
public class MainViewModel : ViewModelBase { }
|
240
|
-
|
241
|
-
}
|
121
|
+
}
|
242
|
-
|
243
|
-
```
|
122
|
+
```
|
244
|
-
|
245
|
-
|
246
123
|
|
247
124
|
### Sub1View
|
248
|
-
|
249
|
-
```x
|
125
|
+
```xml
|
250
|
-
|
251
126
|
<UserControl
|
252
|
-
|
253
127
|
x:Class="Questions374820.Views.Sub1View"
|
254
|
-
|
255
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
128
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
256
|
-
|
257
129
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
258
|
-
|
259
130
|
<Grid>
|
260
|
-
|
261
131
|
<TextBlock
|
262
|
-
|
263
132
|
HorizontalAlignment="Center"
|
264
|
-
|
265
133
|
VerticalAlignment="Center"
|
266
|
-
|
267
134
|
FontSize="36"
|
268
|
-
|
269
135
|
Text="Sub1View" />
|
270
|
-
|
271
136
|
</Grid>
|
272
|
-
|
273
137
|
</UserControl>
|
274
|
-
|
275
|
-
```
|
138
|
+
```
|
276
|
-
|
277
|
-
```
|
139
|
+
```cs
|
278
|
-
|
279
|
-
using System.Windows.Controls;
|
140
|
+
using System.Windows.Controls;
|
280
|
-
|
281
|
-
|
282
141
|
|
283
142
|
namespace Questions374820.Views
|
284
|
-
|
285
|
-
{
|
143
|
+
{
|
286
|
-
|
287
144
|
public partial class Sub1View : UserControl
|
288
|
-
|
289
|
-
{
|
145
|
+
{
|
290
|
-
|
291
146
|
public Sub1View() => InitializeComponent();
|
292
|
-
|
293
|
-
}
|
147
|
+
}
|
294
|
-
|
295
|
-
}
|
148
|
+
}
|
296
|
-
|
297
|
-
```
|
149
|
+
```
|
298
|
-
|
299
|
-
```
|
150
|
+
```cs
|
300
|
-
|
301
|
-
namespace Questions374820.ViewModels
|
151
|
+
namespace Questions374820.ViewModels
|
302
|
-
|
303
|
-
{
|
152
|
+
{
|
304
|
-
|
305
153
|
public class Sub1ViewModel : ViewModelBase { }
|
306
|
-
|
307
|
-
}
|
154
|
+
}
|
308
|
-
|
309
|
-
```
|
155
|
+
```
|
310
|
-
|
311
|
-
|
312
156
|
|
313
157
|
### Sub2View
|
314
|
-
|
315
|
-
```x
|
158
|
+
```xml
|
316
|
-
|
317
159
|
<UserControl
|
318
|
-
|
319
160
|
x:Class="Questions374820.Views.Sub2View"
|
320
|
-
|
321
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
161
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
322
|
-
|
323
162
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
324
|
-
|
325
163
|
<Grid>
|
326
|
-
|
327
164
|
<TextBlock
|
328
|
-
|
329
165
|
HorizontalAlignment="Center"
|
330
|
-
|
331
166
|
VerticalAlignment="Center"
|
332
|
-
|
333
167
|
FontSize="36"
|
334
|
-
|
335
168
|
Text="Sub2View" />
|
336
|
-
|
337
169
|
</Grid>
|
338
|
-
|
339
170
|
</UserControl>
|
340
|
-
|
341
|
-
```
|
171
|
+
```
|
342
|
-
|
343
|
-
```
|
172
|
+
```cs
|
344
|
-
|
345
|
-
using System.Windows.Controls;
|
173
|
+
using System.Windows.Controls;
|
346
|
-
|
347
|
-
|
348
174
|
|
349
175
|
namespace Questions374820.Views
|
350
|
-
|
351
|
-
{
|
176
|
+
{
|
352
|
-
|
353
177
|
public partial class Sub2View : UserControl
|
354
|
-
|
355
|
-
{
|
178
|
+
{
|
356
|
-
|
357
179
|
public Sub2View() => InitializeComponent();
|
358
|
-
|
359
|
-
}
|
180
|
+
}
|
360
|
-
|
361
|
-
}
|
181
|
+
}
|
362
|
-
|
363
|
-
```
|
182
|
+
```
|
364
|
-
|
365
|
-
```
|
183
|
+
```cs
|
366
|
-
|
367
|
-
namespace Questions374820.ViewModels
|
184
|
+
namespace Questions374820.ViewModels
|
368
|
-
|
369
|
-
{
|
185
|
+
{
|
370
|
-
|
371
186
|
public class Sub2ViewModel : ViewModelBase { }
|
372
|
-
|
373
|
-
}
|
187
|
+
}
|
374
|
-
|
375
|
-
```
|
188
|
+
```
|
376
|
-
|
377
|
-
|
378
189
|
|
379
190
|
### ViewModelBase
|
380
|
-
|
381
|
-
```
|
191
|
+
```cs
|
382
|
-
|
383
192
|
using System.ComponentModel;
|
384
|
-
|
385
193
|
using System.Runtime.CompilerServices;
|
386
194
|
|
387
|
-
|
388
|
-
|
389
|
-
namespace Questions374820.ViewModels
|
195
|
+
namespace Questions374820.ViewModels
|
390
|
-
|
391
|
-
{
|
196
|
+
{
|
392
|
-
|
393
197
|
public class ViewModelBase : INotifyPropertyChanged
|
394
|
-
|
395
|
-
{
|
198
|
+
{
|
396
|
-
|
397
199
|
public event PropertyChangedEventHandler PropertyChanged;
|
398
|
-
|
399
200
|
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
|
400
|
-
|
401
201
|
{
|
402
|
-
|
403
202
|
if (Equals(storage, value)) return false;
|
404
|
-
|
405
203
|
storage = value;
|
406
|
-
|
407
204
|
OnPropertyChanged(propertyName);
|
408
|
-
|
409
205
|
return true;
|
410
|
-
|
411
206
|
}
|
412
|
-
|
413
207
|
protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
414
|
-
|
415
|
-
}
|
208
|
+
}
|
416
|
-
|
417
|
-
}
|
209
|
+
}
|
418
|
-
|
419
|
-
```
|
210
|
+
```
|