回答編集履歴

1

見直しキャンペーン中

2023/07/28 14:26

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,303 +1,152 @@
1
1
  参考コードがある場合は、質問に**明示**してください。
2
-
3
2
  [(WPF) SelectedItems をViewModelで取得する - まめ - たんたんめん](https://p4j4.hatenablog.com/entry/2018/03/28/204953)
4
-
5
-
6
-
7
3
 
8
4
 
9
5
  > 以下のコード内の「SelectedStudents」にアイテム群が入らずにnullのままです。
10
6
 
11
-
12
-
13
7
  `SelectedItemsBehavior`では、`SelectedItems`が`IEnumerable`と定義されています(実体は`object[]`です)
14
-
15
8
  `ObservableCollection<Student>`にはキャストできませんので`null`になります。
16
-
17
-
18
9
 
19
10
  ViewModel側もそれに合わせて`IEnumerable`とする必要があります。
20
11
 
21
-
22
-
23
12
  そもそもこのビヘイビアは選択が変わるたびに、`SelectedItems`が丸ごと入れ替わります。
24
-
25
13
  そのため`ObservableCollection`の意味はありません。
26
-
27
-
28
14
 
29
15
  そしてこのコードではViewでの変更しか感知できません(`TwoWay`指定になっていますが^^;
30
16
 
31
-
32
-
33
- ```xaml
17
+ ```xml
34
-
35
18
  <Window
36
-
37
19
  x:Class="Questions351149.MainWindow"
38
-
39
20
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
40
-
41
21
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
42
-
43
22
  xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
44
-
45
23
  xmlns:local="clr-namespace:Questions351149"
46
-
47
24
  Width="800"
48
-
49
25
  Height="450">
50
-
51
26
  <Window.DataContext>
52
-
53
27
  <local:BM_StudentListViewModel />
54
-
55
28
  </Window.DataContext>
56
-
57
29
  <DockPanel Margin="10">
58
-
59
30
  <Button
60
-
61
31
  MinWidth="60"
62
-
63
32
  Margin="10"
64
-
65
33
  HorizontalAlignment="Right"
66
-
67
34
  Command="{Binding OkButton}"
68
-
69
35
  Content="読込"
70
-
71
36
  DockPanel.Dock="Bottom" />
72
37
 
73
-
74
-
75
38
  <ListView ItemsSource="{Binding Students}" SelectionMode="Multiple">
76
-
77
39
  <i:Interaction.Behaviors>
78
-
79
40
  <local:SelectedItemsBehavior SelectedItems="{Binding SelectedStudents}" />
80
-
81
41
  </i:Interaction.Behaviors>
82
-
83
42
  <ListView.View>
84
-
85
43
  <GridView>
86
-
87
44
  <GridViewColumn DisplayMemberBinding="{Binding Id}" Header="番号" />
88
-
89
45
  <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="氏名" />
90
-
91
46
  <GridViewColumn DisplayMemberBinding="{Binding Kana}" Header="フリガナ" />
92
-
93
47
  </GridView>
94
-
95
48
  </ListView.View>
96
-
97
49
  </ListView>
98
-
99
50
  </DockPanel>
100
-
101
51
  </Window>
102
-
103
52
  ```
104
53
 
105
-
106
-
107
- ```C#
54
+ ```cs
108
-
109
55
  using Microsoft.Xaml.Behaviors;
110
-
111
56
  using Prism.Commands;
112
-
113
57
  using Prism.Mvvm;
114
-
115
58
  using System.Collections;
116
-
117
59
  using System.Collections.ObjectModel;
118
-
119
60
  using System.Diagnostics;
120
-
121
61
  using System.Linq;
122
-
123
62
  using System.Windows;
124
-
125
63
  using System.Windows.Controls;
126
-
127
64
  using System.Windows.Controls.Primitives;
128
65
 
129
-
130
-
131
66
  namespace Questions351149
132
-
133
67
  {
134
-
135
68
  [TypeConstraint(typeof(Selector))]
136
-
137
69
  public class SelectedItemsBehavior : Behavior<Selector>
138
-
139
70
  {
140
-
141
71
  public static readonly DependencyProperty SelectedItemsProperty =
142
-
143
72
  DependencyProperty.Register(nameof(SelectedItems),
144
-
145
73
  typeof(IEnumerable),
146
-
147
74
  typeof(SelectedItemsBehavior),
148
-
149
75
  new FrameworkPropertyMetadata(null,
150
-
151
76
  FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
152
-
153
77
  public IEnumerable SelectedItems
154
-
155
78
  {
156
-
157
79
  get => (IEnumerable)GetValue(SelectedItemsProperty);
158
-
159
80
  set => SetValue(SelectedItemsProperty, value);
160
-
161
81
  }
162
-
163
82
  protected override void OnAttached()
164
-
165
83
  {
166
-
167
84
  base.OnAttached();
168
-
169
85
  AssociatedObject.SelectionChanged += SelectionChanged;
170
-
171
86
  }
172
-
173
87
  protected override void OnDetaching()
174
-
175
88
  {
176
-
177
89
  AssociatedObject.SelectionChanged -= SelectionChanged;
178
-
179
90
  base.OnDetaching();
180
-
181
91
  }
182
-
183
92
  private void SelectionChanged(object sender, SelectionChangedEventArgs args)
184
-
185
93
  {
186
-
187
94
  dynamic selector = AssociatedObject;
188
-
189
95
  SelectedItems = Enumerable.ToArray(selector.SelectedItems);
190
-
191
96
  }
192
-
193
97
  }
194
98
 
195
-
196
-
197
99
  public class BM_StudentListViewModel : BindableBase
198
-
199
100
  {
200
-
201
101
  public ObservableCollection<Student> Students { get; }
202
102
 
203
-
204
-
205
103
  private IEnumerable _selectedStudents;
206
-
207
104
  public IEnumerable SelectedStudents
208
-
209
105
  {
210
-
211
106
  get => _selectedStudents;
212
-
213
107
  set => SetProperty(ref _selectedStudents, value);
214
-
215
108
  }
216
-
217
-
218
109
 
219
110
  public DelegateCommand OkButton { get; }
220
111
 
221
112
 
222
-
223
-
224
-
225
113
  public BM_StudentListViewModel()
226
-
227
114
  {
228
-
229
115
  Students = new ObservableCollection<Student>(Enumerable.Range(0, 50).Select(_ => new Student()));
230
-
231
116
  OkButton = new DelegateCommand(OkButtonExecute);
232
-
233
117
  }
234
118
 
119
+ private void OkButtonExecute()
120
+ {
121
+ if (SelectedStudents == null) return;
122
+ foreach (Student s in SelectedStudents)
123
+ {
124
+ Debug.WriteLine(s);
125
+ }
126
+ Debug.WriteLine("");
127
+ }
128
+ }
235
129
 
130
+ public class Student
131
+ {
132
+ public string Id { get; }
133
+ public string Name { get; }
134
+ public string Kana { get; }
236
135
 
237
- private void OkButtonExecute()
136
+ private static int no;
238
-
137
+ public Student()
239
138
  {
240
-
241
- if (SelectedStudents == null) return;
139
+ Id = $"Id{++no}";
242
-
243
- foreach (Student s in SelectedStudents)
244
-
245
- {
246
-
247
- Debug.WriteLine(s);
248
-
249
- }
250
-
251
- Debug.WriteLine("");
140
+ Name = $"Name{no}";
252
-
141
+ Kana = $"Kana{no}";
253
142
  }
254
143
 
144
+ public override string ToString() => $"Id:{Id}, Name:{Name}, Kana:{Kana}";
255
145
  }
256
146
 
257
-
258
-
259
- public class Student
147
+ public partial class MainWindow : Window
260
-
261
148
  {
262
-
263
- public string Id { get; }
264
-
265
- public string Name { get; }
149
+ public MainWindow() => InitializeComponent();
266
-
267
- public string Kana { get; }
268
-
269
-
270
-
271
- private static int no;
272
-
273
- public Student()
274
-
275
- {
276
-
277
- Id = $"Id{++no}";
278
-
279
- Name = $"Name{no}";
280
-
281
- Kana = $"Kana{no}";
282
-
283
- }
284
-
285
-
286
-
287
- public override string ToString() => $"Id:{Id}, Name:{Name}, Kana:{Kana}";
288
-
289
150
  }
290
-
291
-
292
-
293
- public partial class MainWindow : Window
294
-
295
- {
296
-
297
- public MainWindow() => InitializeComponent();
298
-
299
- }
300
-
301
151
  }
302
-
303
152
  ```