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

回答編集履歴

1

見直しキャンペーン中

2023/07/23 04:58

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,252 +1,252 @@
1
- `Settings.Designer.cs`を編集すれば可能です。
2
- が、`Settings.settings`のデザイナをいじったりすると消えやすいので、別に書いたほうが安全です(`partial`なので)
3
-
4
- `TaskList`が`MainWindowViewModel`経由になっている深い意味はありません。
5
-
6
- ```xaml
7
- <Window
8
- x:Class="Questions288214.Views.MainWindow"
9
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
10
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
11
- xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
12
- xmlns:prism="http://prismlibrary.com/"
13
- xmlns:properties="clr-namespace:Questions288214.Properties"
14
- Width="{Binding Width, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
15
- Height="{Binding Height, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
16
- prism:ViewModelLocator.AutoWireViewModel="True"
17
- Left="{Binding Left, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
18
- Top="{Binding Top, Mode=TwoWay, Source={x:Static properties:Settings.Default}}">
19
- <Window.Resources>
20
- <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
21
- <Style TargetType="Label">
22
- <Setter Property="Margin" Value="5" />
23
- </Style>
24
- <Style TargetType="TextBox">
25
- <Setter Property="Margin" Value="5" />
26
- <Setter Property="VerticalAlignment" Value="Center" />
27
- <Setter Property="TextWrapping" Value="Wrap" />
28
- </Style>
29
- </Window.Resources>
30
- <i:Interaction.Triggers>
31
- <i:EventTrigger EventName="Closing">
32
- <prism:InvokeCommandAction Command="{Binding WindowClosingCommand}" />
33
- </i:EventTrigger>
34
- </i:Interaction.Triggers>
35
- <Grid>
36
- <Grid.ColumnDefinitions>
37
- <ColumnDefinition />
38
- <ColumnDefinition Width="2*" />
39
- </Grid.ColumnDefinitions>
40
- <Grid.RowDefinitions>
41
- <RowDefinition Height="Auto" />
42
- <RowDefinition />
43
- </Grid.RowDefinitions>
44
-
45
- <Grid>
46
- <Grid.ColumnDefinitions>
47
- <ColumnDefinition Width="Auto" />
48
- <ColumnDefinition />
49
- </Grid.ColumnDefinitions>
50
- <Label VerticalAlignment="Center" Content="表題" />
51
- <TextBox Grid.Column="1" Text="{Binding Txt_Title.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
52
- </Grid>
53
-
54
- <Grid Grid.Column="1">
55
- <Grid.ColumnDefinitions>
56
- <ColumnDefinition Width="Auto" />
57
- <ColumnDefinition />
58
- <ColumnDefinition Width="Auto" />
59
- </Grid.ColumnDefinitions>
60
- <Label VerticalAlignment="Center" Content="Memo" />
61
- <TextBox Grid.Column="1" Text="{Binding Txt_Memo.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
62
- <Button
63
- Grid.Column="2"
64
- MinWidth="56"
65
- Margin="5"
66
- Command="{Binding AddListCommand}"
67
- Content="追加" />
68
- </Grid>
69
-
70
- <ListBox
71
- x:Name="lst_Title"
72
- Grid.Row="1"
73
- Margin="5"
74
- DisplayMemberPath="Title"
75
- IsSynchronizedWithCurrentItem="True"
76
- ItemsSource="{Binding TaskList}"
77
- SelectionMode="Multiple">
78
- <ListBox.ItemContainerStyle>
79
- <Style TargetType="ListBoxItem">
80
- <Setter Property="IsSelected" Value="{Binding IsSelected.Value}" />
81
- </Style>
82
- </ListBox.ItemContainerStyle>
83
- </ListBox>
84
-
85
- <ListBox
86
- Grid.Row="1"
87
- Grid.Column="1"
88
- Margin="5"
89
- ItemsSource="{Binding TaskList}">
90
- <ListBox.ItemContainerStyle>
91
- <Style TargetType="ListBoxItem">
92
- <Setter Property="Visibility" Value="{Binding IsSelected.Value, Converter={StaticResource BooleanToVisibilityConverter}}" />
93
- </Style>
94
- </ListBox.ItemContainerStyle>
95
- <ListBox.ItemTemplate>
96
- <DataTemplate>
97
- <StackPanel>
98
- <TextBlock FontWeight="Bold" Text="{Binding Title}" />
99
- <TextBlock Text="{Binding Memo}" />
100
- </StackPanel>
101
- </DataTemplate>
102
- </ListBox.ItemTemplate>
103
- </ListBox>
104
- </Grid>
105
- </Window>
106
- ```
107
-
108
- ```C#
109
- using System.Collections.ObjectModel;
110
- using System.Reactive.Linq;
111
- using Prism.Mvvm;
112
- using Reactive.Bindings;
113
-
114
- namespace Questions288214.ViewModels
115
- {
116
- public class MainWindowViewModel : BindableBase
117
- {
118
- public ReactiveProperty<string> Txt_Title { get; set; } = new ReactiveProperty<string>();
119
- public ReactiveProperty<string> Txt_Memo { get; set; } = new ReactiveProperty<string>();
120
-
121
- public ObservableCollection<Task> TaskList
122
- {
123
- get => Properties.Settings.Default.TaskList;
124
- private set => Properties.Settings.Default.TaskList = value;
125
- }
126
-
127
- public ReactiveCommand AddListCommand { get; }
128
- public ReactiveCommand WindowClosingCommand { get; } = new ReactiveCommand();
129
-
130
-
131
- public MainWindowViewModel()
132
- {
133
- if(TaskList == null) TaskList = new ObservableCollection<Task>();
134
-
135
- AddListCommand = Txt_Title
136
- .Select(x => !string.IsNullOrEmpty(x))
137
- .ToReactiveCommand();
138
-
139
- AddListCommand.Subscribe(() =>
140
- {
141
- TaskList.Add(new Task { Title = Txt_Title.Value, Memo = Txt_Memo.Value, });
142
- Txt_Title.Value = "";
143
- Txt_Memo.Value = "";
144
- });
145
-
146
- WindowClosingCommand.Subscribe(() => Properties.Settings.Default.Save());
147
- }
148
- }
149
-
150
- public class Task
151
- {
152
- public string Title { get; set; }
153
- public string Memo { get; set; }
154
- public ReactiveProperty<bool> IsSelected { get; set; } = new ReactiveProperty<bool>();
155
- }
156
- }
157
-
158
- // 別ファイルに書けば安心
159
- namespace Questions288214.Properties
160
- {
161
- using Questions288214.ViewModels;
162
-
163
- internal sealed partial class Settings
164
- {
165
- [System.Configuration.UserScopedSetting()]
166
- [System.Diagnostics.DebuggerNonUserCode()]
167
- public ObservableCollection<Task> TaskList
168
- {
169
- get => (ObservableCollection<Task>)this[nameof(TaskList)];
170
- set => this[nameof(TaskList)] = value;
171
- }
172
- }
173
- }
174
- ```
175
-
176
- ウィンドウ位置保存用は普通にデザイナで入れたとする
177
- ```C#
178
- //------------------------------------------------------------------------------
179
- // <auto-generated>
180
- // このコードはツールによって生成されました。
181
- // ランタイム バージョン:4.0.30319.42000
182
- //
183
- // このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
184
- // コードが再生成されるときに損失したりします。
185
- // </auto-generated>
186
- //------------------------------------------------------------------------------
187
-
188
- namespace Questions288214.Properties {
189
-
190
-
191
- [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
192
- [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")]
193
- internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
194
-
195
- private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
196
-
197
- public static Settings Default {
198
- get {
199
- return defaultInstance;
200
- }
201
- }
202
-
203
- [global::System.Configuration.UserScopedSettingAttribute()]
204
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
205
- [global::System.Configuration.DefaultSettingValueAttribute("NaN")]
206
- public double Top {
207
- get {
208
- return ((double)(this["Top"]));
209
- }
210
- set {
211
- this["Top"] = value;
212
- }
213
- }
214
-
215
- [global::System.Configuration.UserScopedSettingAttribute()]
216
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
217
- [global::System.Configuration.DefaultSettingValueAttribute("NaN")]
218
- public double Left {
219
- get {
220
- return ((double)(this["Left"]));
221
- }
222
- set {
223
- this["Left"] = value;
224
- }
225
- }
226
-
227
- [global::System.Configuration.UserScopedSettingAttribute()]
228
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
229
- [global::System.Configuration.DefaultSettingValueAttribute("525")]
230
- public double Width {
231
- get {
232
- return ((double)(this["Width"]));
233
- }
234
- set {
235
- this["Width"] = value;
236
- }
237
- }
238
-
239
- [global::System.Configuration.UserScopedSettingAttribute()]
240
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
241
- [global::System.Configuration.DefaultSettingValueAttribute("350")]
242
- public double Height {
243
- get {
244
- return ((double)(this["Height"]));
245
- }
246
- set {
247
- this["Height"] = value;
248
- }
249
- }
250
- }
251
- }
1
+ `Settings.Designer.cs`を編集すれば可能です。
2
+ が、`Settings.settings`のデザイナをいじったりすると消えやすいので、別に書いたほうが安全です(`partial`なので)
3
+
4
+ `TaskList`が`MainWindowViewModel`経由になっている深い意味はありません。
5
+
6
+ ```xml
7
+ <Window
8
+ x:Class="Questions288214.Views.MainWindow"
9
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
10
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
11
+ xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
12
+ xmlns:prism="http://prismlibrary.com/"
13
+ xmlns:properties="clr-namespace:Questions288214.Properties"
14
+ Width="{Binding Width, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
15
+ Height="{Binding Height, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
16
+ prism:ViewModelLocator.AutoWireViewModel="True"
17
+ Left="{Binding Left, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
18
+ Top="{Binding Top, Mode=TwoWay, Source={x:Static properties:Settings.Default}}">
19
+ <Window.Resources>
20
+ <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
21
+ <Style TargetType="Label">
22
+ <Setter Property="Margin" Value="5" />
23
+ </Style>
24
+ <Style TargetType="TextBox">
25
+ <Setter Property="Margin" Value="5" />
26
+ <Setter Property="VerticalAlignment" Value="Center" />
27
+ <Setter Property="TextWrapping" Value="Wrap" />
28
+ </Style>
29
+ </Window.Resources>
30
+ <i:Interaction.Triggers>
31
+ <i:EventTrigger EventName="Closing">
32
+ <prism:InvokeCommandAction Command="{Binding WindowClosingCommand}" />
33
+ </i:EventTrigger>
34
+ </i:Interaction.Triggers>
35
+ <Grid>
36
+ <Grid.ColumnDefinitions>
37
+ <ColumnDefinition />
38
+ <ColumnDefinition Width="2*" />
39
+ </Grid.ColumnDefinitions>
40
+ <Grid.RowDefinitions>
41
+ <RowDefinition Height="Auto" />
42
+ <RowDefinition />
43
+ </Grid.RowDefinitions>
44
+
45
+ <Grid>
46
+ <Grid.ColumnDefinitions>
47
+ <ColumnDefinition Width="Auto" />
48
+ <ColumnDefinition />
49
+ </Grid.ColumnDefinitions>
50
+ <Label VerticalAlignment="Center" Content="表題" />
51
+ <TextBox Grid.Column="1" Text="{Binding Txt_Title.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
52
+ </Grid>
53
+
54
+ <Grid Grid.Column="1">
55
+ <Grid.ColumnDefinitions>
56
+ <ColumnDefinition Width="Auto" />
57
+ <ColumnDefinition />
58
+ <ColumnDefinition Width="Auto" />
59
+ </Grid.ColumnDefinitions>
60
+ <Label VerticalAlignment="Center" Content="Memo" />
61
+ <TextBox Grid.Column="1" Text="{Binding Txt_Memo.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
62
+ <Button
63
+ Grid.Column="2"
64
+ MinWidth="56"
65
+ Margin="5"
66
+ Command="{Binding AddListCommand}"
67
+ Content="追加" />
68
+ </Grid>
69
+
70
+ <ListBox
71
+ x:Name="lst_Title"
72
+ Grid.Row="1"
73
+ Margin="5"
74
+ DisplayMemberPath="Title"
75
+ IsSynchronizedWithCurrentItem="True"
76
+ ItemsSource="{Binding TaskList}"
77
+ SelectionMode="Multiple">
78
+ <ListBox.ItemContainerStyle>
79
+ <Style TargetType="ListBoxItem">
80
+ <Setter Property="IsSelected" Value="{Binding IsSelected.Value}" />
81
+ </Style>
82
+ </ListBox.ItemContainerStyle>
83
+ </ListBox>
84
+
85
+ <ListBox
86
+ Grid.Row="1"
87
+ Grid.Column="1"
88
+ Margin="5"
89
+ ItemsSource="{Binding TaskList}">
90
+ <ListBox.ItemContainerStyle>
91
+ <Style TargetType="ListBoxItem">
92
+ <Setter Property="Visibility" Value="{Binding IsSelected.Value, Converter={StaticResource BooleanToVisibilityConverter}}" />
93
+ </Style>
94
+ </ListBox.ItemContainerStyle>
95
+ <ListBox.ItemTemplate>
96
+ <DataTemplate>
97
+ <StackPanel>
98
+ <TextBlock FontWeight="Bold" Text="{Binding Title}" />
99
+ <TextBlock Text="{Binding Memo}" />
100
+ </StackPanel>
101
+ </DataTemplate>
102
+ </ListBox.ItemTemplate>
103
+ </ListBox>
104
+ </Grid>
105
+ </Window>
106
+ ```
107
+
108
+ ```cs
109
+ using System.Collections.ObjectModel;
110
+ using System.Reactive.Linq;
111
+ using Prism.Mvvm;
112
+ using Reactive.Bindings;
113
+
114
+ namespace Questions288214.ViewModels
115
+ {
116
+ public class MainWindowViewModel : BindableBase
117
+ {
118
+ public ReactiveProperty<string> Txt_Title { get; set; } = new ReactiveProperty<string>();
119
+ public ReactiveProperty<string> Txt_Memo { get; set; } = new ReactiveProperty<string>();
120
+
121
+ public ObservableCollection<Task> TaskList
122
+ {
123
+ get => Properties.Settings.Default.TaskList;
124
+ private set => Properties.Settings.Default.TaskList = value;
125
+ }
126
+
127
+ public ReactiveCommand AddListCommand { get; }
128
+ public ReactiveCommand WindowClosingCommand { get; } = new ReactiveCommand();
129
+
130
+
131
+ public MainWindowViewModel()
132
+ {
133
+ if(TaskList == null) TaskList = new ObservableCollection<Task>();
134
+
135
+ AddListCommand = Txt_Title
136
+ .Select(x => !string.IsNullOrEmpty(x))
137
+ .ToReactiveCommand();
138
+
139
+ AddListCommand.Subscribe(() =>
140
+ {
141
+ TaskList.Add(new Task { Title = Txt_Title.Value, Memo = Txt_Memo.Value, });
142
+ Txt_Title.Value = "";
143
+ Txt_Memo.Value = "";
144
+ });
145
+
146
+ WindowClosingCommand.Subscribe(() => Properties.Settings.Default.Save());
147
+ }
148
+ }
149
+
150
+ public class Task
151
+ {
152
+ public string Title { get; set; }
153
+ public string Memo { get; set; }
154
+ public ReactiveProperty<bool> IsSelected { get; set; } = new ReactiveProperty<bool>();
155
+ }
156
+ }
157
+
158
+ // 別ファイルに書けば安心
159
+ namespace Questions288214.Properties
160
+ {
161
+ using Questions288214.ViewModels;
162
+
163
+ internal sealed partial class Settings
164
+ {
165
+ [System.Configuration.UserScopedSetting()]
166
+ [System.Diagnostics.DebuggerNonUserCode()]
167
+ public ObservableCollection<Task> TaskList
168
+ {
169
+ get => (ObservableCollection<Task>)this[nameof(TaskList)];
170
+ set => this[nameof(TaskList)] = value;
171
+ }
172
+ }
173
+ }
174
+ ```
175
+
176
+ ウィンドウ位置保存用は普通にデザイナで入れたとする
177
+ ```cs
178
+ //------------------------------------------------------------------------------
179
+ // <auto-generated>
180
+ // このコードはツールによって生成されました。
181
+ // ランタイム バージョン:4.0.30319.42000
182
+ //
183
+ // このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
184
+ // コードが再生成されるときに損失したりします。
185
+ // </auto-generated>
186
+ //------------------------------------------------------------------------------
187
+
188
+ namespace Questions288214.Properties {
189
+
190
+
191
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
192
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")]
193
+ internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
194
+
195
+ private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
196
+
197
+ public static Settings Default {
198
+ get {
199
+ return defaultInstance;
200
+ }
201
+ }
202
+
203
+ [global::System.Configuration.UserScopedSettingAttribute()]
204
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
205
+ [global::System.Configuration.DefaultSettingValueAttribute("NaN")]
206
+ public double Top {
207
+ get {
208
+ return ((double)(this["Top"]));
209
+ }
210
+ set {
211
+ this["Top"] = value;
212
+ }
213
+ }
214
+
215
+ [global::System.Configuration.UserScopedSettingAttribute()]
216
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
217
+ [global::System.Configuration.DefaultSettingValueAttribute("NaN")]
218
+ public double Left {
219
+ get {
220
+ return ((double)(this["Left"]));
221
+ }
222
+ set {
223
+ this["Left"] = value;
224
+ }
225
+ }
226
+
227
+ [global::System.Configuration.UserScopedSettingAttribute()]
228
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
229
+ [global::System.Configuration.DefaultSettingValueAttribute("525")]
230
+ public double Width {
231
+ get {
232
+ return ((double)(this["Width"]));
233
+ }
234
+ set {
235
+ this["Width"] = value;
236
+ }
237
+ }
238
+
239
+ [global::System.Configuration.UserScopedSettingAttribute()]
240
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
241
+ [global::System.Configuration.DefaultSettingValueAttribute("350")]
242
+ public double Height {
243
+ get {
244
+ return ((double)(this["Height"]));
245
+ }
246
+ set {
247
+ this["Height"] = value;
248
+ }
249
+ }
250
+ }
251
+ }
252
252
  ```