回答編集履歴

2

見直しキャンペーン中

2023/07/23 04:01

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,349 +1,175 @@
1
1
  > 選択されているというのが重要な情報の場合(ファイルに選択情報も保存して次回復元する等)は、別の手のほうがいいかもしれません。
2
2
 
3
-
4
-
5
3
  > Taskクラスにbool IsSelectedプロパティを用意して、lst_TitleではListBoxItemのIsSelectedにバインドし、lst_DetailではListBoxItemのVisibilityにバインドします。
6
-
7
4
  > どちらのListBoxもTaskListがソースですが、lst_Detailでは未選択のものは無いように見えます。
8
-
9
5
  > ちょっとずるいようですが、割とポピュラーな手段だと思います。もし気になるようでしたら追記しますので言ってください。
10
-
11
-
12
6
 
13
7
  1万字に収まらなかったのでこちらで失礼します。
14
8
 
15
-
16
-
17
- ```xaml
9
+ ```xml
18
-
19
10
  <Window
20
-
21
11
  x:Class="Questions284783.Views.MainWindow"
22
-
23
12
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
24
-
25
13
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
26
-
27
14
  xmlns:controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
28
-
29
15
  xmlns:prism="http://prismlibrary.com/"
30
-
31
16
  Width="525"
32
-
33
17
  Height="350"
34
-
35
18
  prism:ViewModelLocator.AutoWireViewModel="True">
36
-
37
19
  <Window.Resources>
38
-
39
20
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
40
-
41
21
  </Window.Resources>
42
-
43
22
  <Grid>
44
-
45
23
  <Grid.ColumnDefinitions>
46
-
47
24
  <ColumnDefinition />
48
-
49
25
  <ColumnDefinition Width="2*" />
50
-
51
26
  </Grid.ColumnDefinitions>
52
-
53
27
  <Grid.RowDefinitions>
54
-
55
28
  <RowDefinition Height="Auto" />
56
-
57
29
  <RowDefinition />
58
-
59
30
  </Grid.RowDefinitions>
60
-
61
31
  <Grid>
62
-
63
32
  <Grid.ColumnDefinitions>
64
-
65
33
  <ColumnDefinition Width="Auto" />
66
-
67
34
  <ColumnDefinition />
68
-
69
35
  </Grid.ColumnDefinitions>
70
-
71
36
  <Label
72
-
73
37
  Margin="5"
74
-
75
38
  VerticalAlignment="Center"
76
-
77
39
  Content="表題" />
78
-
79
40
  <TextBox
80
-
81
41
  Grid.Column="1"
82
-
83
42
  Margin="5"
84
-
85
43
  VerticalAlignment="Center"
86
-
87
44
  Text="{Binding Txt_Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
88
-
89
45
  TextWrapping="Wrap" />
90
-
91
46
  </Grid>
92
-
93
47
  <Grid Grid.Column="1">
94
-
95
48
  <Grid.ColumnDefinitions>
96
-
97
49
  <ColumnDefinition Width="Auto" />
98
-
99
50
  <ColumnDefinition />
100
-
101
51
  <ColumnDefinition Width="Auto" />
102
-
103
52
  </Grid.ColumnDefinitions>
104
-
105
53
  <Label
106
-
107
54
  Margin="5"
108
-
109
55
  VerticalAlignment="Center"
110
-
111
56
  Content="Memo" />
112
-
113
57
  <TextBox
114
-
115
58
  Grid.Column="1"
116
-
117
59
  Margin="5"
118
-
119
60
  VerticalAlignment="Center"
120
-
121
61
  Text="{Binding Txt_Memo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
122
-
123
62
  TextWrapping="Wrap" />
124
-
125
63
  <Button
126
-
127
64
  Grid.Column="2"
128
-
129
65
  MinWidth="56"
130
-
131
66
  Margin="5"
132
-
133
67
  Command="{Binding ExecuteCommand}"
134
-
135
68
  Content="追加" />
136
-
137
69
  </Grid>
138
-
139
70
  <Grid Grid.Row="1">
140
-
141
71
  <Grid.RowDefinitions>
142
-
143
72
  <RowDefinition Height="Auto" />
144
-
145
73
  <RowDefinition />
146
-
147
74
  </Grid.RowDefinitions>
148
-
149
75
  <ComboBox
150
-
151
76
  x:Name="comboBox"
152
-
153
77
  Margin="5"
154
-
155
78
  SelectedIndex="0">
156
-
157
79
  <controls:SelectionMode>Single</controls:SelectionMode>
158
-
159
80
  <controls:SelectionMode>Multiple</controls:SelectionMode>
160
-
161
81
  <controls:SelectionMode>Extended</controls:SelectionMode>
162
-
163
82
  </ComboBox>
164
-
165
83
  <ListBox
166
-
167
84
  x:Name="lst_Title"
168
-
169
85
  Grid.Row="1"
170
-
171
86
  Margin="5"
172
-
173
87
  DisplayMemberPath="Title"
174
-
175
88
  IsSynchronizedWithCurrentItem="True"
176
-
177
89
  ItemsSource="{Binding TaskList}"
178
-
179
90
  SelectionMode="{Binding SelectedValue, ElementName=comboBox}">
180
-
181
91
  <ListBox.ItemContainerStyle>
182
-
183
92
  <Style TargetType="ListBoxItem">
184
-
185
93
  <Setter Property="IsSelected" Value="{Binding IsSelected}" />
186
-
187
94
  </Style>
188
-
189
95
  </ListBox.ItemContainerStyle>
190
-
191
96
  </ListBox>
192
-
193
97
  </Grid>
194
-
195
98
  <ListBox
196
-
197
99
  Grid.Row="1"
198
-
199
100
  Grid.Column="1"
200
-
201
101
  Margin="5"
202
-
203
102
  ItemsSource="{Binding TaskList}">
204
-
205
103
  <ListBox.ItemContainerStyle>
206
-
207
104
  <Style TargetType="ListBoxItem">
208
-
209
105
  <Setter Property="Visibility" Value="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" />
210
-
211
106
  </Style>
212
-
213
107
  </ListBox.ItemContainerStyle>
214
-
215
108
  <ListBox.ItemTemplate>
216
-
217
109
  <DataTemplate>
218
-
219
110
  <StackPanel>
220
-
221
111
  <TextBlock FontWeight="Bold" Text="{Binding Title}" />
222
-
223
112
  <TextBlock Text="{Binding Detail}" />
224
-
225
113
  </StackPanel>
226
-
227
114
  </DataTemplate>
228
-
229
115
  </ListBox.ItemTemplate>
230
-
231
116
  </ListBox>
232
-
233
117
  </Grid>
234
-
235
118
  </Window>
236
-
237
119
  ```
238
120
 
239
-
240
-
241
- ```C#
121
+ ```cs
242
-
243
122
  using System.Collections.ObjectModel;
244
-
245
123
  using Prism.Commands;
246
-
247
124
  using Prism.Mvvm;
248
125
 
249
-
250
-
251
126
  namespace Questions284783.ViewModels
252
-
253
127
  {
254
-
255
128
  public class MainWindowViewModel : BindableBase
256
-
257
129
  {
258
-
259
130
  private string txt_Title;
260
-
261
131
  public string Txt_Title
262
-
263
132
  {
264
-
265
133
  get => txt_Title;
266
-
267
134
  set { if(SetProperty(ref txt_Title, value)) ExecuteCommand.RaiseCanExecuteChanged(); }
268
-
269
135
  }
270
136
 
271
-
272
-
273
137
  private string txt_Memo;
274
-
275
138
  public string Txt_Memo
276
-
277
139
  {
278
-
279
140
  get => txt_Memo;
280
-
281
141
  set { if(SetProperty(ref txt_Memo, value)) ExecuteCommand.RaiseCanExecuteChanged(); }
282
-
283
142
  }
284
143
 
285
-
286
-
287
144
  public ObservableCollection<Task> TaskList { get; } = new ObservableCollection<Task>();
288
-
289
145
  public DelegateCommand ExecuteCommand { get; private set; }
290
146
 
291
-
292
-
293
147
  public MainWindowViewModel()
294
-
295
148
  {
296
-
297
149
  TaskList.Add(new Task { Title = "aaa", Detail = "111" });
298
-
299
150
  TaskList.Add(new Task { Title = "bbb", Detail = "222" });
300
-
301
151
  TaskList.Add(new Task { Title = "ccc", Detail = "333" });
302
-
303
152
  ExecuteCommand = new DelegateCommand(Execute, CanExecute);
304
-
305
153
  }
306
154
 
307
-
308
-
309
155
  private void Execute()
310
-
311
156
  {
312
-
313
157
  TaskList.Add(new Task { Title = Txt_Title, Detail = Txt_Memo });
314
-
315
158
  Txt_Title = "";
316
-
317
159
  Txt_Memo = "";
318
-
319
160
  ExecuteCommand.RaiseCanExecuteChanged();
320
-
321
161
  }
322
162
 
323
-
324
-
325
163
  private bool CanExecute() => !string.IsNullOrEmpty(Txt_Title);
326
-
327
164
  }
328
165
 
329
-
330
-
331
166
  public class Task : BindableBase
332
-
333
167
  {
334
-
335
168
  public string Title { get; set; }
336
-
337
169
  public string Detail { get; set; }
338
170
 
339
-
340
-
341
171
  private bool isSelected;
342
-
343
172
  public bool IsSelected { get => isSelected; set => SetProperty(ref isSelected, value); }
344
-
345
173
  }
346
-
347
174
  }
348
-
349
175
  ```

1

流れの説明

2020/08/15 07:55

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,11 +1,19 @@
1
+ > 選択されているというのが重要な情報の場合(ファイルに選択情報も保存して次回復元する等)は、別の手のほうがいいかもしれません。
2
+
3
+
4
+
5
+ > Taskクラスにbool IsSelectedプロパティを用意して、lst_TitleではListBoxItemのIsSelectedにバインドし、lst_DetailではListBoxItemのVisibilityにバインドします。
6
+
7
+ > どちらのListBoxもTaskListがソースですが、lst_Detailでは未選択のものは無いように見えます。
8
+
9
+ > ちょっとずるいようですが、割とポピュラーな手段だと思います。もし気になるようでしたら追記しますので言ってください。
10
+
11
+
12
+
1
13
  1万字に収まらなかったのでこちらで失礼します。
2
14
 
3
15
 
4
16
 
5
- IsSelectedにバインドパターン
6
-
7
-
8
-
9
17
  ```xaml
10
18
 
11
19
  <Window