回答編集履歴

1

見直しキャンペーン中

2023/07/23 04:58

投稿

TN8001
TN8001

スコア9359

test CHANGED
@@ -1,503 +1,252 @@
1
1
  `Settings.Designer.cs`を編集すれば可能です。
2
-
3
2
  が、`Settings.settings`のデザイナをいじったりすると消えやすいので、別に書いたほうが安全です(`partial`なので)
4
3
 
5
-
6
-
7
4
  `TaskList`が`MainWindowViewModel`経由になっている深い意味はありません。
8
5
 
9
-
10
-
11
- ```xaml
6
+ ```xml
12
-
13
7
  <Window
14
-
15
8
  x:Class="Questions288214.Views.MainWindow"
16
-
17
9
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
18
-
19
10
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
20
-
21
11
  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
22
-
23
12
  xmlns:prism="http://prismlibrary.com/"
24
-
25
13
  xmlns:properties="clr-namespace:Questions288214.Properties"
26
-
27
14
  Width="{Binding Width, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
28
-
29
15
  Height="{Binding Height, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
30
-
31
16
  prism:ViewModelLocator.AutoWireViewModel="True"
32
-
33
17
  Left="{Binding Left, Mode=TwoWay, Source={x:Static properties:Settings.Default}}"
34
-
35
18
  Top="{Binding Top, Mode=TwoWay, Source={x:Static properties:Settings.Default}}">
36
-
37
19
  <Window.Resources>
38
-
39
20
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
40
-
41
21
  <Style TargetType="Label">
42
-
43
22
  <Setter Property="Margin" Value="5" />
44
-
45
23
  </Style>
46
-
47
24
  <Style TargetType="TextBox">
48
-
49
25
  <Setter Property="Margin" Value="5" />
50
-
51
26
  <Setter Property="VerticalAlignment" Value="Center" />
52
-
53
27
  <Setter Property="TextWrapping" Value="Wrap" />
54
-
55
28
  </Style>
56
-
57
29
  </Window.Resources>
58
-
59
30
  <i:Interaction.Triggers>
60
-
61
31
  <i:EventTrigger EventName="Closing">
62
-
63
32
  <prism:InvokeCommandAction Command="{Binding WindowClosingCommand}" />
64
-
65
33
  </i:EventTrigger>
66
-
67
34
  </i:Interaction.Triggers>
68
-
69
35
  <Grid>
70
-
71
36
  <Grid.ColumnDefinitions>
72
-
73
37
  <ColumnDefinition />
74
-
75
38
  <ColumnDefinition Width="2*" />
76
-
77
39
  </Grid.ColumnDefinitions>
78
-
79
40
  <Grid.RowDefinitions>
80
-
81
41
  <RowDefinition Height="Auto" />
82
-
83
42
  <RowDefinition />
84
-
85
43
  </Grid.RowDefinitions>
86
44
 
87
-
88
-
89
45
  <Grid>
90
-
91
46
  <Grid.ColumnDefinitions>
92
-
93
47
  <ColumnDefinition Width="Auto" />
94
-
95
48
  <ColumnDefinition />
96
-
97
49
  </Grid.ColumnDefinitions>
98
-
99
50
  <Label VerticalAlignment="Center" Content="表題" />
100
-
101
51
  <TextBox Grid.Column="1" Text="{Binding Txt_Title.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
102
-
103
52
  </Grid>
104
53
 
105
-
106
-
107
54
  <Grid Grid.Column="1">
108
-
109
55
  <Grid.ColumnDefinitions>
110
-
111
56
  <ColumnDefinition Width="Auto" />
112
-
113
57
  <ColumnDefinition />
114
-
115
58
  <ColumnDefinition Width="Auto" />
116
-
117
59
  </Grid.ColumnDefinitions>
118
-
119
60
  <Label VerticalAlignment="Center" Content="Memo" />
120
-
121
61
  <TextBox Grid.Column="1" Text="{Binding Txt_Memo.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
122
-
123
62
  <Button
124
-
125
63
  Grid.Column="2"
126
-
127
64
  MinWidth="56"
128
-
129
65
  Margin="5"
130
-
131
66
  Command="{Binding AddListCommand}"
132
-
133
67
  Content="追加" />
134
-
135
68
  </Grid>
136
69
 
137
-
138
-
139
70
  <ListBox
140
-
141
71
  x:Name="lst_Title"
142
-
143
72
  Grid.Row="1"
144
-
145
73
  Margin="5"
146
-
147
74
  DisplayMemberPath="Title"
148
-
149
75
  IsSynchronizedWithCurrentItem="True"
150
-
151
76
  ItemsSource="{Binding TaskList}"
152
-
153
77
  SelectionMode="Multiple">
154
-
155
78
  <ListBox.ItemContainerStyle>
156
-
157
79
  <Style TargetType="ListBoxItem">
158
-
159
80
  <Setter Property="IsSelected" Value="{Binding IsSelected.Value}" />
160
-
161
81
  </Style>
162
-
163
82
  </ListBox.ItemContainerStyle>
164
-
165
83
  </ListBox>
166
84
 
167
-
168
-
169
85
  <ListBox
170
-
171
86
  Grid.Row="1"
172
-
173
87
  Grid.Column="1"
174
-
175
88
  Margin="5"
176
-
177
89
  ItemsSource="{Binding TaskList}">
178
-
179
90
  <ListBox.ItemContainerStyle>
180
-
181
91
  <Style TargetType="ListBoxItem">
182
-
183
92
  <Setter Property="Visibility" Value="{Binding IsSelected.Value, Converter={StaticResource BooleanToVisibilityConverter}}" />
184
-
185
93
  </Style>
186
-
187
94
  </ListBox.ItemContainerStyle>
188
-
189
95
  <ListBox.ItemTemplate>
190
-
191
96
  <DataTemplate>
192
-
193
97
  <StackPanel>
194
-
195
98
  <TextBlock FontWeight="Bold" Text="{Binding Title}" />
196
-
197
99
  <TextBlock Text="{Binding Memo}" />
198
-
199
100
  </StackPanel>
200
-
201
101
  </DataTemplate>
202
-
203
102
  </ListBox.ItemTemplate>
204
-
205
103
  </ListBox>
206
-
207
104
  </Grid>
208
-
209
105
  </Window>
210
-
211
106
  ```
212
107
 
213
-
214
-
215
- ```C#
108
+ ```cs
216
-
217
109
  using System.Collections.ObjectModel;
218
-
219
110
  using System.Reactive.Linq;
220
-
221
111
  using Prism.Mvvm;
222
-
223
112
  using Reactive.Bindings;
224
113
 
225
-
226
-
227
114
  namespace Questions288214.ViewModels
228
-
229
115
  {
230
-
231
116
  public class MainWindowViewModel : BindableBase
232
-
233
117
  {
234
-
235
118
  public ReactiveProperty<string> Txt_Title { get; set; } = new ReactiveProperty<string>();
236
-
237
119
  public ReactiveProperty<string> Txt_Memo { get; set; } = new ReactiveProperty<string>();
238
120
 
239
-
240
-
241
121
  public ObservableCollection<Task> TaskList
242
-
243
122
  {
244
-
245
123
  get => Properties.Settings.Default.TaskList;
246
-
247
124
  private set => Properties.Settings.Default.TaskList = value;
248
-
249
- }
125
+ }
250
-
251
-
252
126
 
253
127
  public ReactiveCommand AddListCommand { get; }
254
-
255
128
  public ReactiveCommand WindowClosingCommand { get; } = new ReactiveCommand();
256
129
 
257
130
 
258
-
259
-
260
-
261
131
  public MainWindowViewModel()
262
-
263
132
  {
264
-
265
133
  if(TaskList == null) TaskList = new ObservableCollection<Task>();
266
134
 
267
-
268
-
269
135
  AddListCommand = Txt_Title
270
-
271
136
  .Select(x => !string.IsNullOrEmpty(x))
272
-
273
137
  .ToReactiveCommand();
274
138
 
275
-
276
-
277
139
  AddListCommand.Subscribe(() =>
278
-
279
140
  {
280
-
281
141
  TaskList.Add(new Task { Title = Txt_Title.Value, Memo = Txt_Memo.Value, });
282
-
283
142
  Txt_Title.Value = "";
284
-
285
143
  Txt_Memo.Value = "";
286
-
287
144
  });
288
145
 
289
-
290
-
291
146
  WindowClosingCommand.Subscribe(() => Properties.Settings.Default.Save());
292
-
293
- }
147
+ }
294
-
295
- }
148
+ }
296
-
297
-
298
149
 
299
150
  public class Task
300
-
301
151
  {
302
-
303
152
  public string Title { get; set; }
304
-
305
153
  public string Memo { get; set; }
306
-
307
154
  public ReactiveProperty<bool> IsSelected { get; set; } = new ReactiveProperty<bool>();
308
-
309
- }
155
+ }
310
-
311
156
  }
312
157
 
313
-
314
-
315
158
  // 別ファイルに書けば安心
316
-
317
159
  namespace Questions288214.Properties
318
-
319
160
  {
320
-
321
161
  using Questions288214.ViewModels;
322
162
 
323
-
324
-
325
163
  internal sealed partial class Settings
326
-
327
164
  {
328
-
329
165
  [System.Configuration.UserScopedSetting()]
330
-
331
166
  [System.Diagnostics.DebuggerNonUserCode()]
332
-
333
167
  public ObservableCollection<Task> TaskList
334
-
335
168
  {
336
-
337
169
  get => (ObservableCollection<Task>)this[nameof(TaskList)];
338
-
339
170
  set => this[nameof(TaskList)] = value;
340
-
341
- }
171
+ }
342
-
343
- }
172
+ }
344
-
345
173
  }
346
-
347
174
  ```
348
175
 
349
-
350
-
351
176
  ウィンドウ位置保存用は普通にデザイナで入れたとする
352
-
353
- ```C#
177
+ ```cs
354
-
355
178
  //------------------------------------------------------------------------------
356
-
357
179
  // <auto-generated>
358
-
359
180
  // このコードはツールによって生成されました。
360
-
361
181
  // ランタイム バージョン:4.0.30319.42000
362
-
363
182
  //
364
-
365
183
  // このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
366
-
367
184
  // コードが再生成されるときに損失したりします。
368
-
369
185
  // </auto-generated>
370
-
371
186
  //------------------------------------------------------------------------------
372
187
 
373
-
374
-
375
188
  namespace Questions288214.Properties {
376
-
377
189
 
378
-
379
190
 
380
-
381
191
  [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
382
-
383
192
  [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")]
384
-
385
193
  internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
386
-
387
-
388
-
194
+
389
195
  private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
390
-
391
-
392
-
196
+
393
197
  public static Settings Default {
394
-
395
- get {
198
+ get {
396
-
397
199
  return defaultInstance;
398
-
399
- }
200
+ }
400
-
401
- }
201
+ }
402
-
403
-
404
-
202
+
405
- [global::System.Configuration.UserScopedSettingAttribute()]
203
+ [global::System.Configuration.UserScopedSettingAttribute()]
406
-
407
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
204
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
408
-
409
205
  [global::System.Configuration.DefaultSettingValueAttribute("NaN")]
410
-
411
206
  public double Top {
412
-
413
- get {
207
+ get {
414
-
415
208
  return ((double)(this["Top"]));
416
-
417
- }
209
+ }
418
-
419
- set {
210
+ set {
420
-
421
211
  this["Top"] = value;
422
-
423
- }
212
+ }
424
-
425
- }
213
+ }
426
-
427
-
428
-
214
+
429
- [global::System.Configuration.UserScopedSettingAttribute()]
215
+ [global::System.Configuration.UserScopedSettingAttribute()]
430
-
431
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
216
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
432
-
433
217
  [global::System.Configuration.DefaultSettingValueAttribute("NaN")]
434
-
435
218
  public double Left {
436
-
437
- get {
219
+ get {
438
-
439
220
  return ((double)(this["Left"]));
440
-
441
- }
221
+ }
442
-
443
- set {
222
+ set {
444
-
445
223
  this["Left"] = value;
446
-
447
- }
224
+ }
448
-
449
- }
225
+ }
450
-
451
-
452
-
226
+
453
- [global::System.Configuration.UserScopedSettingAttribute()]
227
+ [global::System.Configuration.UserScopedSettingAttribute()]
454
-
455
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
228
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
456
-
457
229
  [global::System.Configuration.DefaultSettingValueAttribute("525")]
458
-
459
230
  public double Width {
460
-
461
- get {
231
+ get {
462
-
463
232
  return ((double)(this["Width"]));
464
-
465
- }
233
+ }
466
-
467
- set {
234
+ set {
468
-
469
235
  this["Width"] = value;
470
-
471
- }
236
+ }
472
-
473
- }
237
+ }
474
-
475
-
476
-
238
+
477
- [global::System.Configuration.UserScopedSettingAttribute()]
239
+ [global::System.Configuration.UserScopedSettingAttribute()]
478
-
479
- [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
240
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
480
-
481
241
  [global::System.Configuration.DefaultSettingValueAttribute("350")]
482
-
483
242
  public double Height {
484
-
485
- get {
243
+ get {
486
-
487
244
  return ((double)(this["Height"]));
488
-
489
- }
245
+ }
490
-
491
- set {
246
+ set {
492
-
493
247
  this["Height"] = value;
494
-
495
- }
248
+ }
496
-
497
- }
249
+ }
498
-
499
- }
250
+ }
500
-
501
251
  }
502
-
503
252
  ```