質問編集履歴
1
ソースコード追加/バージョン情報追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
WPF Setting
|
1
|
+
WPF Settings.settings コレクション保存
|
test
CHANGED
@@ -6,15 +6,11 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
|
9
|
+
|
10
|
-
|
10
|
+
|
11
|
-
|
11
|
+
表題/Memoを入力して、追加すると、下のリストボックスに内容が追加されるアプリを作成しています。
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
|
17
|
-
|
13
|
+
左側リストボックスを選択すると、右側のリストボックスに内容が表示される。
|
18
14
|
|
19
15
|
[![イメージ説明](6a9e6aab74884327c142053378a9bbf8.jpeg)]
|
20
16
|
|
@@ -24,8 +20,340 @@
|
|
24
20
|
|
25
21
|
アプリ内で追加したリスト内容を保存し、アプリを再起動しても追加した内容が出てくるようにしたいです。
|
26
22
|
|
27
|
-
Setting
|
23
|
+
Settings.settingsを使用するのが一番楽かと思いますが、
|
28
24
|
|
29
25
|
リストボックスは、ソースコード内ではコレクションをバインドしています。
|
30
26
|
|
31
|
-
Setting
|
27
|
+
Settings.settingsにコレクションを追加することはできるのでしょうか?
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
### 該当のソースコード
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
```C#
|
38
|
+
|
39
|
+
using Prism.Mvvm;
|
40
|
+
|
41
|
+
using System;
|
42
|
+
|
43
|
+
using System.ComponentModel;
|
44
|
+
|
45
|
+
using Reactive.Bindings;
|
46
|
+
|
47
|
+
using System.Linq;
|
48
|
+
|
49
|
+
using System.Reactive.Linq;
|
50
|
+
|
51
|
+
using System.Collections.ObjectModel;
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
namespace ListBoxデータ保存.ViewModels
|
56
|
+
|
57
|
+
{
|
58
|
+
|
59
|
+
public class MainWindowViewModel : BindableBase
|
60
|
+
|
61
|
+
{
|
62
|
+
|
63
|
+
public ReactiveProperty<string> Txt_Title { get; set; } = new ReactiveProperty<string>();
|
64
|
+
|
65
|
+
public ReactiveProperty<string> Txt_Memo { get; set; } = new ReactiveProperty<string>();
|
66
|
+
|
67
|
+
public ObservableCollection<Task> TaskList { get; set; }
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
//コマンド
|
74
|
+
|
75
|
+
public ReactiveCommand AddListCommand { get; private set; }
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
public MainWindowViewModel()
|
80
|
+
|
81
|
+
{
|
82
|
+
|
83
|
+
TaskList = new ObservableCollection<Task>();
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
//コマンド生成
|
88
|
+
|
89
|
+
AddListCommand = Txt_Title
|
90
|
+
|
91
|
+
.Select(x => !string.IsNullOrEmpty(x))
|
92
|
+
|
93
|
+
.ToReactiveCommand();
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
//コマンド動作定義
|
98
|
+
|
99
|
+
AddListCommand.Subscribe(_ =>
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
TaskList.Add(new Task { Title = Txt_Title.Value, Memo = Txt_Memo.Value });
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
Txt_Title.Value = "";
|
108
|
+
|
109
|
+
Txt_Memo.Value = "";
|
110
|
+
|
111
|
+
});
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
public class Task : BindableBase
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
public string Title { get; set; }
|
126
|
+
|
127
|
+
public string Memo { get; set; }
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
public ReactiveProperty<bool> IsSelected { get; set; } = new ReactiveProperty<bool>();
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
```XAML
|
144
|
+
|
145
|
+
<Window x:Class="ListBoxデータ保存.Views.MainWindow"
|
146
|
+
|
147
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
148
|
+
|
149
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
150
|
+
|
151
|
+
xmlns:prism="http://prismlibrary.com/"
|
152
|
+
|
153
|
+
prism:ViewModelLocator.AutoWireViewModel="True"
|
154
|
+
|
155
|
+
Title="{Binding Title}" Height="350" Width="525" >
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
<Window.Resources>
|
160
|
+
|
161
|
+
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
|
162
|
+
|
163
|
+
</Window.Resources>
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
<Grid>
|
168
|
+
|
169
|
+
<Grid.ColumnDefinitions>
|
170
|
+
|
171
|
+
<ColumnDefinition />
|
172
|
+
|
173
|
+
<ColumnDefinition Width="2*" />
|
174
|
+
|
175
|
+
</Grid.ColumnDefinitions>
|
176
|
+
|
177
|
+
<Grid.RowDefinitions>
|
178
|
+
|
179
|
+
<RowDefinition Height="Auto" />
|
180
|
+
|
181
|
+
<RowDefinition />
|
182
|
+
|
183
|
+
</Grid.RowDefinitions>
|
184
|
+
|
185
|
+
<Grid>
|
186
|
+
|
187
|
+
<Grid.ColumnDefinitions>
|
188
|
+
|
189
|
+
<ColumnDefinition Width="Auto" />
|
190
|
+
|
191
|
+
<ColumnDefinition />
|
192
|
+
|
193
|
+
</Grid.ColumnDefinitions>
|
194
|
+
|
195
|
+
<Label
|
196
|
+
|
197
|
+
Margin="5"
|
198
|
+
|
199
|
+
VerticalAlignment="Center"
|
200
|
+
|
201
|
+
Content="表題" HorizontalAlignment="Right" Width="34" />
|
202
|
+
|
203
|
+
<TextBox
|
204
|
+
|
205
|
+
Grid.Column="1"
|
206
|
+
|
207
|
+
VerticalAlignment="Center"
|
208
|
+
|
209
|
+
Text="{Binding Txt_Title.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
210
|
+
|
211
|
+
TextWrapping="Wrap" Margin="10,0,10,0" />
|
212
|
+
|
213
|
+
</Grid>
|
214
|
+
|
215
|
+
<Grid Grid.Column="1">
|
216
|
+
|
217
|
+
<Grid.ColumnDefinitions>
|
218
|
+
|
219
|
+
<ColumnDefinition Width="Auto" />
|
220
|
+
|
221
|
+
<ColumnDefinition />
|
222
|
+
|
223
|
+
<ColumnDefinition Width="Auto" />
|
224
|
+
|
225
|
+
</Grid.ColumnDefinitions>
|
226
|
+
|
227
|
+
<Label
|
228
|
+
|
229
|
+
Margin="5"
|
230
|
+
|
231
|
+
VerticalAlignment="Center"
|
232
|
+
|
233
|
+
Content="Memo" HorizontalAlignment="Right" Width="44" />
|
234
|
+
|
235
|
+
<TextBox
|
236
|
+
|
237
|
+
Grid.Column="1"
|
238
|
+
|
239
|
+
Margin="5"
|
240
|
+
|
241
|
+
VerticalAlignment="Center"
|
242
|
+
|
243
|
+
Text="{Binding Txt_Memo.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
244
|
+
|
245
|
+
TextWrapping="Wrap" />
|
246
|
+
|
247
|
+
<Button
|
248
|
+
|
249
|
+
Grid.Column="2"
|
250
|
+
|
251
|
+
MinWidth="56"
|
252
|
+
|
253
|
+
Margin="5"
|
254
|
+
|
255
|
+
Command="{Binding AddListCommand}"
|
256
|
+
|
257
|
+
Content="追加" HorizontalAlignment="Left" Width="56" />
|
258
|
+
|
259
|
+
</Grid>
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
<Grid Grid.Row="1">
|
264
|
+
|
265
|
+
<ListBox
|
266
|
+
|
267
|
+
x:Name="lst_Title"
|
268
|
+
|
269
|
+
Margin="5"
|
270
|
+
|
271
|
+
ItemsSource="{Binding TaskList}"
|
272
|
+
|
273
|
+
DisplayMemberPath="Title"
|
274
|
+
|
275
|
+
IsSynchronizedWithCurrentItem="True"
|
276
|
+
|
277
|
+
SelectionMode="Extended">
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
<ListBox.ItemContainerStyle>
|
282
|
+
|
283
|
+
<Style TargetType="ListBoxItem">
|
284
|
+
|
285
|
+
<Setter Property="IsSelected" Value="{Binding IsSelected.Value}" />
|
286
|
+
|
287
|
+
</Style>
|
288
|
+
|
289
|
+
</ListBox.ItemContainerStyle>
|
290
|
+
|
291
|
+
</ListBox>
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
</Grid>
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
<Grid Grid.Row="1" Grid.Column="1" >
|
300
|
+
|
301
|
+
<ListBox
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
Margin="5"
|
306
|
+
|
307
|
+
ItemsSource="{Binding TaskList}">
|
308
|
+
|
309
|
+
<ListBox.ItemContainerStyle>
|
310
|
+
|
311
|
+
<Style TargetType="ListBoxItem">
|
312
|
+
|
313
|
+
<Setter Property="Visibility" Value="{Binding IsSelected.Value, Converter={StaticResource BooleanToVisibilityConverter}}" />
|
314
|
+
|
315
|
+
</Style>
|
316
|
+
|
317
|
+
</ListBox.ItemContainerStyle>
|
318
|
+
|
319
|
+
<ListBox.ItemTemplate>
|
320
|
+
|
321
|
+
<DataTemplate>
|
322
|
+
|
323
|
+
<StackPanel>
|
324
|
+
|
325
|
+
<TextBlock FontWeight="Bold" Text="{Binding Title}" />
|
326
|
+
|
327
|
+
<TextBlock Text="{Binding Memo}" />
|
328
|
+
|
329
|
+
</StackPanel>
|
330
|
+
|
331
|
+
</DataTemplate>
|
332
|
+
|
333
|
+
</ListBox.ItemTemplate>
|
334
|
+
|
335
|
+
</ListBox>
|
336
|
+
|
337
|
+
</Grid>
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
</Grid>
|
342
|
+
|
343
|
+
</Window>
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
```
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
### 補足情報(FW/ツールのバージョンなど)
|
354
|
+
|
355
|
+
OS:win10
|
356
|
+
|
357
|
+
.NET Framework:4.7.2
|
358
|
+
|
359
|
+
Visual Studio 2019
|