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

質問編集履歴

1

ソースコード追加/バージョン情報追加

2020/08/29 02:42

投稿

tomiieee
tomiieee

スコア27

title CHANGED
@@ -1,1 +1,1 @@
1
- WPF Settingファイル コレクション保存
1
+ WPF Settings.settings コレクション保存
body CHANGED
@@ -2,15 +2,179 @@
2
2
  C# WPF 初心者です。
3
3
  用語の誤用/的外れな質問ありましたらご容赦ください。
4
4
 
5
- 前回の質問の続きです。
6
- [前回の質問](https://teratail.com/questions/284783)
7
5
 
8
-
6
+ 表題/Memoを入力して、追加すると、下のリストボックスに内容が追加されるアプリを作成しています。
9
- __左側リストボックスを選択すると、右側のリストボックスに内容が表示される。__
7
+ 左側リストボックスを選択すると、右側のリストボックスに内容が表示される。
10
8
  [![イメージ説明](6a9e6aab74884327c142053378a9bbf8.jpeg)]
11
9
 
12
10
  ### 実現したいこと
13
11
  アプリ内で追加したリスト内容を保存し、アプリを再起動しても追加した内容が出てくるようにしたいです。
14
- Settingファイルを使用するのが一番楽かと思いますが、
12
+ Settings.settingsを使用するのが一番楽かと思いますが、
15
13
  リストボックスは、ソースコード内ではコレクションをバインドしています。
16
- Settingファイルにコレクションを追加することはできるのでしょうか?
14
+ Settings.settingsにコレクションを追加することはできるのでしょうか?
15
+
16
+
17
+ ### 該当のソースコード
18
+
19
+ ```C#
20
+ using Prism.Mvvm;
21
+ using System;
22
+ using System.ComponentModel;
23
+ using Reactive.Bindings;
24
+ using System.Linq;
25
+ using System.Reactive.Linq;
26
+ using System.Collections.ObjectModel;
27
+
28
+ namespace ListBoxデータ保存.ViewModels
29
+ {
30
+ public class MainWindowViewModel : BindableBase
31
+ {
32
+ public ReactiveProperty<string> Txt_Title { get; set; } = new ReactiveProperty<string>();
33
+ public ReactiveProperty<string> Txt_Memo { get; set; } = new ReactiveProperty<string>();
34
+ public ObservableCollection<Task> TaskList { get; set; }
35
+
36
+
37
+ //コマンド
38
+ public ReactiveCommand AddListCommand { get; private set; }
39
+
40
+ public MainWindowViewModel()
41
+ {
42
+ TaskList = new ObservableCollection<Task>();
43
+
44
+ //コマンド生成
45
+ AddListCommand = Txt_Title
46
+ .Select(x => !string.IsNullOrEmpty(x))
47
+ .ToReactiveCommand();
48
+
49
+ //コマンド動作定義
50
+ AddListCommand.Subscribe(_ =>
51
+ {
52
+ TaskList.Add(new Task { Title = Txt_Title.Value, Memo = Txt_Memo.Value });
53
+
54
+ Txt_Title.Value = "";
55
+ Txt_Memo.Value = "";
56
+ });
57
+ }
58
+ }
59
+
60
+
61
+ public class Task : BindableBase
62
+ {
63
+ public string Title { get; set; }
64
+ public string Memo { get; set; }
65
+
66
+ public ReactiveProperty<bool> IsSelected { get; set; } = new ReactiveProperty<bool>();
67
+ }
68
+
69
+ }
70
+
71
+ ```
72
+ ```XAML
73
+ <Window x:Class="ListBoxデータ保存.Views.MainWindow"
74
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
75
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
76
+ xmlns:prism="http://prismlibrary.com/"
77
+ prism:ViewModelLocator.AutoWireViewModel="True"
78
+ Title="{Binding Title}" Height="350" Width="525" >
79
+
80
+ <Window.Resources>
81
+ <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
82
+ </Window.Resources>
83
+
84
+ <Grid>
85
+ <Grid.ColumnDefinitions>
86
+ <ColumnDefinition />
87
+ <ColumnDefinition Width="2*" />
88
+ </Grid.ColumnDefinitions>
89
+ <Grid.RowDefinitions>
90
+ <RowDefinition Height="Auto" />
91
+ <RowDefinition />
92
+ </Grid.RowDefinitions>
93
+ <Grid>
94
+ <Grid.ColumnDefinitions>
95
+ <ColumnDefinition Width="Auto" />
96
+ <ColumnDefinition />
97
+ </Grid.ColumnDefinitions>
98
+ <Label
99
+ Margin="5"
100
+ VerticalAlignment="Center"
101
+ Content="表題" HorizontalAlignment="Right" Width="34" />
102
+ <TextBox
103
+ Grid.Column="1"
104
+ VerticalAlignment="Center"
105
+ Text="{Binding Txt_Title.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
106
+ TextWrapping="Wrap" Margin="10,0,10,0" />
107
+ </Grid>
108
+ <Grid Grid.Column="1">
109
+ <Grid.ColumnDefinitions>
110
+ <ColumnDefinition Width="Auto" />
111
+ <ColumnDefinition />
112
+ <ColumnDefinition Width="Auto" />
113
+ </Grid.ColumnDefinitions>
114
+ <Label
115
+ Margin="5"
116
+ VerticalAlignment="Center"
117
+ Content="Memo" HorizontalAlignment="Right" Width="44" />
118
+ <TextBox
119
+ Grid.Column="1"
120
+ Margin="5"
121
+ VerticalAlignment="Center"
122
+ Text="{Binding Txt_Memo.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
123
+ TextWrapping="Wrap" />
124
+ <Button
125
+ Grid.Column="2"
126
+ MinWidth="56"
127
+ Margin="5"
128
+ Command="{Binding AddListCommand}"
129
+ Content="追加" HorizontalAlignment="Left" Width="56" />
130
+ </Grid>
131
+
132
+ <Grid Grid.Row="1">
133
+ <ListBox
134
+ x:Name="lst_Title"
135
+ Margin="5"
136
+ ItemsSource="{Binding TaskList}"
137
+ DisplayMemberPath="Title"
138
+ IsSynchronizedWithCurrentItem="True"
139
+ SelectionMode="Extended">
140
+
141
+ <ListBox.ItemContainerStyle>
142
+ <Style TargetType="ListBoxItem">
143
+ <Setter Property="IsSelected" Value="{Binding IsSelected.Value}" />
144
+ </Style>
145
+ </ListBox.ItemContainerStyle>
146
+ </ListBox>
147
+
148
+ </Grid>
149
+
150
+ <Grid Grid.Row="1" Grid.Column="1" >
151
+ <ListBox
152
+
153
+ Margin="5"
154
+ ItemsSource="{Binding TaskList}">
155
+ <ListBox.ItemContainerStyle>
156
+ <Style TargetType="ListBoxItem">
157
+ <Setter Property="Visibility" Value="{Binding IsSelected.Value, Converter={StaticResource BooleanToVisibilityConverter}}" />
158
+ </Style>
159
+ </ListBox.ItemContainerStyle>
160
+ <ListBox.ItemTemplate>
161
+ <DataTemplate>
162
+ <StackPanel>
163
+ <TextBlock FontWeight="Bold" Text="{Binding Title}" />
164
+ <TextBlock Text="{Binding Memo}" />
165
+ </StackPanel>
166
+ </DataTemplate>
167
+ </ListBox.ItemTemplate>
168
+ </ListBox>
169
+ </Grid>
170
+
171
+ </Grid>
172
+ </Window>
173
+
174
+ ```
175
+
176
+
177
+ ### 補足情報(FW/ツールのバージョンなど)
178
+ OS:win10
179
+ .NET Framework:4.7.2
180
+ Visual Studio 2019