teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/29 13:00

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,210 +1,210 @@
1
- 申し訳ないですけど、参考サイトはだいぶひどいですね。。。
2
- わかっている人には何の知見も得られませんし、勉強中の方には省略部分が多すぎて参考になりません(今どきは完全版をGitHubに上げてリンクしたりするものですが)
3
-
4
- 記事の意図のプロジェクト構成はこうです。
5
- ![ソリューションエクスプローラー](e326bddd491930ced7b11d200325c7c7.png)
6
-
7
- `ViewModels`・`Views`フォルダ以下に、それぞれの`ViewModel`・`View`があるという前提です。
8
- そのフォルダ(正確には名前空間)を、xmlnsで`vm`・`view`と指定しています。
9
-
10
- 現状フォルダ分けされていないのと、それぞれの`View`がありません(通常`UserControl`です)
11
-
12
- 注)
13
- `MainWindow`も`Views`の中に入れているのかもしれませんが、名前がややこしいのであえて入れていません。
14
- `MainWindowViewModel`もあると思われますが、質問の本題でないので作りません。
15
-
16
- ---
17
- ### MainWindow
18
- ```xaml
19
- <Window
20
- x:Class="Questions374820.MainWindow"
21
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
23
- xmlns:view="clr-namespace:Questions374820.Views"
24
- xmlns:vm="clr-namespace:Questions374820.ViewModels"
25
- Title="MainWindow"
26
- Width="800"
27
- Height="450">
28
- <DockPanel>
29
- <DockPanel.Resources>
30
- <DataTemplate DataType="{x:Type vm:MainViewModel}">
31
- <view:MainView />
32
- </DataTemplate>
33
- <DataTemplate DataType="{x:Type vm:Sub1ViewModel}">
34
- <view:Sub1View />
35
- </DataTemplate>
36
- <DataTemplate DataType="{x:Type vm:Sub2ViewModel}">
37
- <view:Sub2View />
38
- </DataTemplate>
39
- </DockPanel.Resources>
40
- <UniformGrid DockPanel.Dock="Top" Rows="1">
41
- <Button Click="Button_Click" Content="メイン" />
42
- <Button Click="Button_Click" Content="サブ1" />
43
- <Button Click="Button_Click" Content="サブ2" />
44
- </UniformGrid>
45
- <ContentControl Content="{Binding SampleViewModel}" Focusable="False" />
46
- </DockPanel>
47
- </Window>
48
- ```
49
- ```C#
50
- using System.ComponentModel;
51
- using System.Windows;
52
- using System.Windows.Controls;
53
- using Questions374820.ViewModels;
54
-
55
- namespace Questions374820
56
- {
57
- public partial class MainWindow : Window, INotifyPropertyChanged
58
- {
59
- public ViewModelBase SampleViewModel { get; set; } = new MainViewModel();
60
-
61
- public MainWindow()
62
- {
63
- InitializeComponent();
64
- DataContext = this;
65
- }
66
-
67
- private void Button_Click(object sender, RoutedEventArgs e)
68
- {
69
- var 画面 = ((Button)sender).Content as string;
70
- switch (画面)
71
- {
72
- case "メイン":
73
- SampleViewModel = new MainViewModel();
74
- break;
75
- case "サブ1":
76
- SampleViewModel = new Sub1ViewModel();
77
- break;
78
- case "サブ2":
79
- SampleViewModel = new Sub2ViewModel();
80
- break;
81
- }
82
-
83
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SampleViewModel)));
84
- }
85
-
86
- public event PropertyChangedEventHandler PropertyChanged;
87
- }
88
- }
89
- ```
90
-
91
- ### MainView
92
- ```xaml
93
- <UserControl
94
- x:Class="Questions374820.Views.MainView"
95
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
96
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
97
- <Grid>
98
- <TextBlock
99
- HorizontalAlignment="Center"
100
- VerticalAlignment="Center"
101
- FontSize="36"
102
- Text="MainView" />
103
- </Grid>
104
- </UserControl>
105
- ```
106
- ```C#
107
- using System.Windows.Controls;
108
-
109
- namespace Questions374820.Views
110
- {
111
- public partial class MainView : UserControl
112
- {
113
- public MainView() => InitializeComponent();
114
- }
115
- }
116
- ```
117
- ```C#
118
- namespace Questions374820.ViewModels
119
- {
120
- public class MainViewModel : ViewModelBase { }
121
- }
122
- ```
123
-
124
- ### Sub1View
125
- ```xaml
126
- <UserControl
127
- x:Class="Questions374820.Views.Sub1View"
128
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
129
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
130
- <Grid>
131
- <TextBlock
132
- HorizontalAlignment="Center"
133
- VerticalAlignment="Center"
134
- FontSize="36"
135
- Text="Sub1View" />
136
- </Grid>
137
- </UserControl>
138
- ```
139
- ```C#
140
- using System.Windows.Controls;
141
-
142
- namespace Questions374820.Views
143
- {
144
- public partial class Sub1View : UserControl
145
- {
146
- public Sub1View() => InitializeComponent();
147
- }
148
- }
149
- ```
150
- ```C#
151
- namespace Questions374820.ViewModels
152
- {
153
- public class Sub1ViewModel : ViewModelBase { }
154
- }
155
- ```
156
-
157
- ### Sub2View
158
- ```xaml
159
- <UserControl
160
- x:Class="Questions374820.Views.Sub2View"
161
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
162
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
163
- <Grid>
164
- <TextBlock
165
- HorizontalAlignment="Center"
166
- VerticalAlignment="Center"
167
- FontSize="36"
168
- Text="Sub2View" />
169
- </Grid>
170
- </UserControl>
171
- ```
172
- ```C#
173
- using System.Windows.Controls;
174
-
175
- namespace Questions374820.Views
176
- {
177
- public partial class Sub2View : UserControl
178
- {
179
- public Sub2View() => InitializeComponent();
180
- }
181
- }
182
- ```
183
- ```C#
184
- namespace Questions374820.ViewModels
185
- {
186
- public class Sub2ViewModel : ViewModelBase { }
187
- }
188
- ```
189
-
190
- ### ViewModelBase
191
- ```C#
192
- using System.ComponentModel;
193
- using System.Runtime.CompilerServices;
194
-
195
- namespace Questions374820.ViewModels
196
- {
197
- public class ViewModelBase : INotifyPropertyChanged
198
- {
199
- public event PropertyChangedEventHandler PropertyChanged;
200
- protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
201
- {
202
- if (Equals(storage, value)) return false;
203
- storage = value;
204
- OnPropertyChanged(propertyName);
205
- return true;
206
- }
207
- protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
208
- }
209
- }
1
+ 申し訳ないですけど、参考サイトはだいぶひどいですね。。。
2
+ わかっている人には何の知見も得られませんし、勉強中の方には省略部分が多すぎて参考になりません(今どきは完全版をGitHubに上げてリンクしたりするものですが)
3
+
4
+ 記事の意図のプロジェクト構成はこうです。
5
+ ![ソリューションエクスプローラー](e326bddd491930ced7b11d200325c7c7.png)
6
+
7
+ `ViewModels`・`Views`フォルダ以下に、それぞれの`ViewModel`・`View`があるという前提です。
8
+ そのフォルダ(正確には名前空間)を、xmlnsで`vm`・`view`と指定しています。
9
+
10
+ 現状フォルダ分けされていないのと、それぞれの`View`がありません(通常`UserControl`です)
11
+
12
+ 注)
13
+ `MainWindow`も`Views`の中に入れているのかもしれませんが、名前がややこしいのであえて入れていません。
14
+ `MainWindowViewModel`もあると思われますが、質問の本題でないので作りません。
15
+
16
+ ---
17
+ ### MainWindow
18
+ ```xml
19
+ <Window
20
+ x:Class="Questions374820.MainWindow"
21
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
23
+ xmlns:view="clr-namespace:Questions374820.Views"
24
+ xmlns:vm="clr-namespace:Questions374820.ViewModels"
25
+ Title="MainWindow"
26
+ Width="800"
27
+ Height="450">
28
+ <DockPanel>
29
+ <DockPanel.Resources>
30
+ <DataTemplate DataType="{x:Type vm:MainViewModel}">
31
+ <view:MainView />
32
+ </DataTemplate>
33
+ <DataTemplate DataType="{x:Type vm:Sub1ViewModel}">
34
+ <view:Sub1View />
35
+ </DataTemplate>
36
+ <DataTemplate DataType="{x:Type vm:Sub2ViewModel}">
37
+ <view:Sub2View />
38
+ </DataTemplate>
39
+ </DockPanel.Resources>
40
+ <UniformGrid DockPanel.Dock="Top" Rows="1">
41
+ <Button Click="Button_Click" Content="メイン" />
42
+ <Button Click="Button_Click" Content="サブ1" />
43
+ <Button Click="Button_Click" Content="サブ2" />
44
+ </UniformGrid>
45
+ <ContentControl Content="{Binding SampleViewModel}" Focusable="False" />
46
+ </DockPanel>
47
+ </Window>
48
+ ```
49
+ ```cs
50
+ using System.ComponentModel;
51
+ using System.Windows;
52
+ using System.Windows.Controls;
53
+ using Questions374820.ViewModels;
54
+
55
+ namespace Questions374820
56
+ {
57
+ public partial class MainWindow : Window, INotifyPropertyChanged
58
+ {
59
+ public ViewModelBase SampleViewModel { get; set; } = new MainViewModel();
60
+
61
+ public MainWindow()
62
+ {
63
+ InitializeComponent();
64
+ DataContext = this;
65
+ }
66
+
67
+ private void Button_Click(object sender, RoutedEventArgs e)
68
+ {
69
+ var 画面 = ((Button)sender).Content as string;
70
+ switch (画面)
71
+ {
72
+ case "メイン":
73
+ SampleViewModel = new MainViewModel();
74
+ break;
75
+ case "サブ1":
76
+ SampleViewModel = new Sub1ViewModel();
77
+ break;
78
+ case "サブ2":
79
+ SampleViewModel = new Sub2ViewModel();
80
+ break;
81
+ }
82
+
83
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SampleViewModel)));
84
+ }
85
+
86
+ public event PropertyChangedEventHandler PropertyChanged;
87
+ }
88
+ }
89
+ ```
90
+
91
+ ### MainView
92
+ ```xml
93
+ <UserControl
94
+ x:Class="Questions374820.Views.MainView"
95
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
96
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
97
+ <Grid>
98
+ <TextBlock
99
+ HorizontalAlignment="Center"
100
+ VerticalAlignment="Center"
101
+ FontSize="36"
102
+ Text="MainView" />
103
+ </Grid>
104
+ </UserControl>
105
+ ```
106
+ ```cs
107
+ using System.Windows.Controls;
108
+
109
+ namespace Questions374820.Views
110
+ {
111
+ public partial class MainView : UserControl
112
+ {
113
+ public MainView() => InitializeComponent();
114
+ }
115
+ }
116
+ ```
117
+ ```cs
118
+ namespace Questions374820.ViewModels
119
+ {
120
+ public class MainViewModel : ViewModelBase { }
121
+ }
122
+ ```
123
+
124
+ ### Sub1View
125
+ ```xml
126
+ <UserControl
127
+ x:Class="Questions374820.Views.Sub1View"
128
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
129
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
130
+ <Grid>
131
+ <TextBlock
132
+ HorizontalAlignment="Center"
133
+ VerticalAlignment="Center"
134
+ FontSize="36"
135
+ Text="Sub1View" />
136
+ </Grid>
137
+ </UserControl>
138
+ ```
139
+ ```cs
140
+ using System.Windows.Controls;
141
+
142
+ namespace Questions374820.Views
143
+ {
144
+ public partial class Sub1View : UserControl
145
+ {
146
+ public Sub1View() => InitializeComponent();
147
+ }
148
+ }
149
+ ```
150
+ ```cs
151
+ namespace Questions374820.ViewModels
152
+ {
153
+ public class Sub1ViewModel : ViewModelBase { }
154
+ }
155
+ ```
156
+
157
+ ### Sub2View
158
+ ```xml
159
+ <UserControl
160
+ x:Class="Questions374820.Views.Sub2View"
161
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
162
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
163
+ <Grid>
164
+ <TextBlock
165
+ HorizontalAlignment="Center"
166
+ VerticalAlignment="Center"
167
+ FontSize="36"
168
+ Text="Sub2View" />
169
+ </Grid>
170
+ </UserControl>
171
+ ```
172
+ ```cs
173
+ using System.Windows.Controls;
174
+
175
+ namespace Questions374820.Views
176
+ {
177
+ public partial class Sub2View : UserControl
178
+ {
179
+ public Sub2View() => InitializeComponent();
180
+ }
181
+ }
182
+ ```
183
+ ```cs
184
+ namespace Questions374820.ViewModels
185
+ {
186
+ public class Sub2ViewModel : ViewModelBase { }
187
+ }
188
+ ```
189
+
190
+ ### ViewModelBase
191
+ ```cs
192
+ using System.ComponentModel;
193
+ using System.Runtime.CompilerServices;
194
+
195
+ namespace Questions374820.ViewModels
196
+ {
197
+ public class ViewModelBase : INotifyPropertyChanged
198
+ {
199
+ public event PropertyChangedEventHandler PropertyChanged;
200
+ protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
201
+ {
202
+ if (Equals(storage, value)) return false;
203
+ storage = value;
204
+ OnPropertyChanged(propertyName);
205
+ return true;
206
+ }
207
+ protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
208
+ }
209
+ }
210
210
  ```