回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,451 +1,226 @@
|
|
1
1
|
ちょっと苦労されているようなので、サンプルを書いてみました。
|
2
|
-
|
3
2
|
NuGetで`Prism.Core`を入れてあります。
|
4
|
-
|
5
|
-
```
|
3
|
+
```cs
|
6
|
-
|
7
4
|
using System.Collections.ObjectModel;
|
8
|
-
|
9
5
|
using System.ComponentModel;
|
10
|
-
|
11
6
|
using System.Windows;
|
12
|
-
|
13
7
|
using System.Windows.Data;
|
14
|
-
|
15
8
|
using Prism.Commands;
|
16
|
-
|
17
9
|
using Prism.Mvvm;
|
18
10
|
|
19
|
-
|
20
|
-
|
21
11
|
namespace Questions222623
|
22
|
-
|
23
12
|
{
|
24
|
-
|
25
13
|
internal class MainViewModel
|
26
|
-
|
27
|
-
{
|
14
|
+
{
|
28
|
-
|
29
15
|
public class ItemViewModel : BindableBase
|
30
|
-
|
31
|
-
{
|
16
|
+
{
|
32
|
-
|
33
17
|
private bool _Selected;
|
34
|
-
|
35
18
|
public bool Selected { get => _Selected; set => SetProperty(ref _Selected, value); }
|
36
19
|
|
37
|
-
|
38
|
-
|
39
20
|
private string _Name;
|
40
|
-
|
41
21
|
public string Name { get => _Name; set => SetProperty(ref _Name, value); }
|
42
|
-
|
43
|
-
}
|
22
|
+
}
|
44
|
-
|
45
|
-
|
46
23
|
|
47
24
|
public ObservableCollection<ItemViewModel> Items { get; }
|
48
|
-
|
49
25
|
public ICollectionView LeftView { get; }
|
50
|
-
|
51
26
|
public ICollectionView RightView { get; }
|
52
|
-
|
53
27
|
public DelegateCommand<ItemViewModel> Left2RightCommand { get; }
|
54
|
-
|
55
28
|
public DelegateCommand<ItemViewModel> Right2LeftCommand { get; }
|
56
29
|
|
57
|
-
|
58
|
-
|
59
30
|
public MainViewModel()
|
60
|
-
|
61
|
-
{
|
31
|
+
{
|
62
|
-
|
63
32
|
Items = new ObservableCollection<ItemViewModel>
|
64
|
-
|
65
|
-
{
|
33
|
+
{
|
66
|
-
|
67
34
|
new ItemViewModel() { Name = "C1", Selected = true },
|
68
|
-
|
69
35
|
new ItemViewModel() { Name = "C2", Selected = false },
|
70
|
-
|
71
36
|
new ItemViewModel() { Name = "C3", Selected = true },
|
72
|
-
|
73
37
|
new ItemViewModel() { Name = "C4", Selected = true },
|
74
|
-
|
75
38
|
new ItemViewModel() { Name = "C5", Selected = false },
|
76
|
-
|
77
39
|
new ItemViewModel() { Name = "C6", Selected = true },
|
78
|
-
|
79
40
|
};
|
80
41
|
|
81
|
-
|
82
|
-
|
83
42
|
var cvs = new CollectionViewSource { Source = Items, };
|
84
|
-
|
85
43
|
cvs.IsLiveFilteringRequested = true; // 変更があったときにフィルターを即時反映
|
86
|
-
|
87
44
|
cvs.LiveFilteringProperties.Add(nameof(ItemViewModel.Selected)); // 変更を監視するプロパティ名
|
88
|
-
|
89
45
|
LeftView = cvs.View;
|
90
|
-
|
91
46
|
LeftView.Filter = x => (x as ItemViewModel).Selected == true;
|
92
47
|
|
93
|
-
|
94
|
-
|
95
48
|
cvs = new CollectionViewSource { Source = Items, };
|
96
|
-
|
97
49
|
cvs.IsLiveFilteringRequested = true;
|
98
|
-
|
99
50
|
cvs.LiveFilteringProperties.Add(nameof(ItemViewModel.Selected));
|
100
|
-
|
101
51
|
RightView = cvs.View;
|
102
|
-
|
103
52
|
RightView.Filter = x => (x as ItemViewModel).Selected == false;
|
104
53
|
|
105
|
-
|
106
|
-
|
107
54
|
Left2RightCommand = new DelegateCommand<ItemViewModel>(Left2rightCommandExecute);
|
108
|
-
|
109
55
|
Right2LeftCommand = new DelegateCommand<ItemViewModel>((item) => { if(item != null) item.Selected = true; }); // ラムダでもよい
|
110
|
-
|
111
|
-
}
|
56
|
+
}
|
112
|
-
|
113
|
-
|
114
57
|
|
115
58
|
private void Left2rightCommandExecute(ItemViewModel item)
|
116
|
-
|
117
|
-
{
|
59
|
+
{
|
118
|
-
|
119
60
|
if(item != null)
|
120
|
-
|
121
61
|
item.Selected = false;
|
122
|
-
|
123
|
-
}
|
62
|
+
}
|
124
|
-
|
125
|
-
}
|
63
|
+
}
|
126
|
-
|
127
|
-
|
128
64
|
|
129
65
|
public partial class MainWindow : Window
|
130
|
-
|
131
|
-
{
|
66
|
+
{
|
132
|
-
|
133
67
|
public MainWindow() => InitializeComponent();
|
134
|
-
|
135
|
-
}
|
68
|
+
}
|
136
|
-
|
137
69
|
}
|
138
|
-
|
139
|
-
```
|
70
|
+
```
|
140
|
-
|
141
|
-
```x
|
71
|
+
```xml
|
142
|
-
|
143
72
|
<Window
|
144
|
-
|
145
73
|
x:Class="Questions222623.MainWindow"
|
146
|
-
|
147
74
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
148
|
-
|
149
75
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
150
|
-
|
151
76
|
xmlns:local="clr-namespace:Questions222623"
|
152
|
-
|
153
77
|
Title="MainWindow"
|
154
|
-
|
155
78
|
Width="800"
|
156
|
-
|
157
79
|
Height="450">
|
158
|
-
|
159
80
|
<Window.DataContext>
|
160
|
-
|
161
81
|
<local:MainViewModel />
|
162
|
-
|
163
82
|
</Window.DataContext>
|
164
|
-
|
165
83
|
<Grid>
|
166
|
-
|
167
84
|
<Grid.ColumnDefinitions>
|
168
|
-
|
169
|
-
<ColumnDefinition />
|
85
|
+
<ColumnDefinition />
|
170
|
-
|
171
86
|
<ColumnDefinition Width="Auto" />
|
172
|
-
|
173
|
-
<ColumnDefinition />
|
87
|
+
<ColumnDefinition />
|
174
|
-
|
175
88
|
</Grid.ColumnDefinitions>
|
176
|
-
|
177
|
-
<ListBox
|
89
|
+
<ListBox
|
178
|
-
|
179
90
|
x:Name="listBox"
|
180
|
-
|
181
|
-
DisplayMemberPath="Name"
|
91
|
+
DisplayMemberPath="Name"
|
182
|
-
|
183
92
|
ItemsSource="{Binding LeftView}" />
|
184
|
-
|
185
93
|
<StackPanel Grid.Column="1" VerticalAlignment="Center">
|
186
|
-
|
187
94
|
<Button
|
188
|
-
|
189
95
|
Margin="5"
|
190
|
-
|
191
96
|
Padding="5"
|
192
|
-
|
193
97
|
Command="{Binding Left2RightCommand}"
|
194
|
-
|
195
98
|
CommandParameter="{Binding SelectedItem, ElementName=listBox}"
|
196
|
-
|
197
99
|
Content="→" />
|
198
|
-
|
199
100
|
<Button
|
200
|
-
|
201
101
|
Margin="5"
|
202
|
-
|
203
102
|
Padding="5"
|
204
|
-
|
205
103
|
Command="{Binding Right2LeftCommand}"
|
206
|
-
|
207
104
|
CommandParameter="{Binding SelectedItem, ElementName=listBox1}"
|
208
|
-
|
209
105
|
Content="←" />
|
210
|
-
|
211
106
|
</StackPanel>
|
212
|
-
|
213
|
-
<ListBox
|
107
|
+
<ListBox
|
214
|
-
|
215
108
|
x:Name="listBox1"
|
216
|
-
|
217
109
|
Grid.Column="2"
|
218
|
-
|
219
|
-
DisplayMemberPath="Name"
|
110
|
+
DisplayMemberPath="Name"
|
220
|
-
|
221
111
|
ItemsSource="{Binding RightView}" />
|
222
|
-
|
223
112
|
</Grid>
|
224
|
-
|
225
113
|
</Window>
|
226
|
-
|
227
|
-
```
|
114
|
+
```
|
228
|
-
|
229
115
|
![イメージ説明](2d99cba7cf1f91078346bcf5a4292aa4.png)
|
230
|
-
|
231
116
|
こういうイメージであっていますでしょうか?
|
232
117
|
|
233
|
-
|
234
|
-
|
235
118
|
---
|
236
119
|
|
237
|
-
|
238
|
-
|
239
120
|
別解としてドラッグ&ドロップでの移動例も作ってみました。
|
240
|
-
|
241
121
|
NuGetで`Prism.Core` `gong-wpf-dragdrop`を入れてあります。
|
242
|
-
|
243
|
-
```
|
122
|
+
```cs
|
244
|
-
|
245
123
|
using System.Collections;
|
246
|
-
|
247
124
|
using System.Collections.ObjectModel;
|
248
|
-
|
249
125
|
using System.Windows;
|
250
|
-
|
251
126
|
using GongSolutions.Wpf.DragDrop;
|
252
|
-
|
253
127
|
using Prism.Mvvm;
|
254
128
|
|
255
|
-
|
256
|
-
|
257
129
|
namespace Questions222623
|
258
|
-
|
259
130
|
{
|
260
|
-
|
261
131
|
internal class MainViewModel : IDropTarget
|
262
|
-
|
263
|
-
{
|
132
|
+
{
|
264
|
-
|
265
133
|
public class ItemViewModel : BindableBase
|
266
|
-
|
267
|
-
{
|
134
|
+
{
|
268
|
-
|
269
135
|
private string _Name;
|
270
|
-
|
271
136
|
public string Name { get => _Name; set => SetProperty(ref _Name, value); }
|
272
|
-
|
273
|
-
}
|
137
|
+
}
|
274
|
-
|
275
|
-
|
276
138
|
|
277
139
|
public ObservableCollection<ItemViewModel> SelectedItems { get; }
|
278
|
-
|
279
140
|
public ObservableCollection<ItemViewModel> UnselectedItems { get; }
|
280
141
|
|
281
|
-
|
282
|
-
|
283
142
|
private readonly DefaultDropHandler defaultDropHandler = new DefaultDropHandler();
|
284
143
|
|
285
|
-
|
286
|
-
|
287
144
|
public MainViewModel()
|
288
|
-
|
289
|
-
{
|
145
|
+
{
|
290
|
-
|
291
146
|
SelectedItems = new ObservableCollection<ItemViewModel>
|
292
|
-
|
293
|
-
{
|
147
|
+
{
|
294
|
-
|
295
148
|
new ItemViewModel() { Name = "C1", },
|
296
|
-
|
297
149
|
new ItemViewModel() { Name = "C3", },
|
298
|
-
|
299
150
|
new ItemViewModel() { Name = "C4", },
|
300
|
-
|
301
151
|
new ItemViewModel() { Name = "C6", },
|
302
|
-
|
303
152
|
};
|
304
|
-
|
305
153
|
UnselectedItems = new ObservableCollection<ItemViewModel>
|
306
|
-
|
307
|
-
{
|
154
|
+
{
|
308
|
-
|
309
155
|
new ItemViewModel() { Name = "C2", },
|
310
|
-
|
311
156
|
new ItemViewModel() { Name = "C5", },
|
312
|
-
|
313
157
|
};
|
314
|
-
|
315
|
-
}
|
158
|
+
}
|
316
|
-
|
317
|
-
|
318
159
|
|
319
160
|
void IDropTarget.DragOver(IDropInfo dropInfo)
|
320
|
-
|
321
|
-
{
|
161
|
+
{
|
322
|
-
|
323
162
|
if(dropInfo.Data is ItemViewModel)
|
324
|
-
|
325
|
-
{
|
163
|
+
{
|
326
|
-
|
327
164
|
dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
|
328
|
-
|
329
165
|
dropInfo.Effects = DragDropEffects.Move;
|
330
|
-
|
331
166
|
}
|
332
|
-
|
333
|
-
}
|
167
|
+
}
|
334
|
-
|
335
168
|
void IDropTarget.Drop(IDropInfo dropInfo)
|
336
|
-
|
337
|
-
{
|
169
|
+
{
|
338
|
-
|
339
170
|
if(dropInfo.Data is ItemViewModel item)
|
340
|
-
|
341
|
-
{
|
171
|
+
{
|
342
|
-
|
343
172
|
if(dropInfo.DragInfo.SourceCollection != dropInfo.TargetCollection)
|
344
|
-
|
345
173
|
{
|
346
|
-
|
347
174
|
((IList)dropInfo.DragInfo.SourceCollection).Remove(item);
|
348
|
-
|
349
175
|
((IList)dropInfo.TargetCollection).Insert(dropInfo.InsertIndex, item);
|
350
|
-
|
351
176
|
}
|
352
|
-
|
353
177
|
else
|
354
|
-
|
355
178
|
{
|
356
|
-
|
357
179
|
defaultDropHandler.Drop(dropInfo);
|
358
|
-
|
359
180
|
}
|
360
|
-
|
361
181
|
}
|
362
|
-
|
363
|
-
}
|
182
|
+
}
|
364
|
-
|
365
|
-
}
|
183
|
+
}
|
366
|
-
|
367
184
|
public partial class MainWindow : Window
|
368
|
-
|
369
|
-
{
|
185
|
+
{
|
370
|
-
|
371
186
|
public MainWindow() => InitializeComponent();
|
372
|
-
|
373
|
-
}
|
187
|
+
}
|
374
|
-
|
375
188
|
}
|
376
|
-
|
377
|
-
```
|
189
|
+
```
|
378
|
-
|
379
|
-
|
380
|
-
|
190
|
+
|
381
|
-
```x
|
191
|
+
```xml
|
382
|
-
|
383
192
|
<Window
|
384
|
-
|
385
193
|
x:Class="Questions222623.MainWindow"
|
386
|
-
|
387
194
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
388
|
-
|
389
195
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
390
|
-
|
391
196
|
xmlns:dd="urn:gong-wpf-dragdrop"
|
392
|
-
|
393
197
|
xmlns:local="clr-namespace:Questions222623"
|
394
|
-
|
395
198
|
Title="MainWindow"
|
396
|
-
|
397
199
|
Width="800"
|
398
|
-
|
399
200
|
Height="450">
|
400
|
-
|
401
201
|
<Window.DataContext>
|
402
|
-
|
403
202
|
<local:MainViewModel />
|
404
|
-
|
405
203
|
</Window.DataContext>
|
406
|
-
|
407
204
|
<Grid>
|
408
|
-
|
409
205
|
<Grid.ColumnDefinitions>
|
410
|
-
|
411
|
-
<ColumnDefinition />
|
206
|
+
<ColumnDefinition />
|
412
|
-
|
413
|
-
<ColumnDefinition />
|
207
|
+
<ColumnDefinition />
|
414
|
-
|
415
208
|
</Grid.ColumnDefinitions>
|
416
|
-
|
417
|
-
<ListBox
|
209
|
+
<ListBox
|
418
|
-
|
419
210
|
dd:DragDrop.DropHandler="{Binding}"
|
420
|
-
|
421
211
|
dd:DragDrop.IsDragSource="True"
|
422
|
-
|
423
212
|
dd:DragDrop.IsDropTarget="True"
|
424
|
-
|
425
|
-
DisplayMemberPath="Name"
|
213
|
+
DisplayMemberPath="Name"
|
426
|
-
|
427
214
|
ItemsSource="{Binding SelectedItems}" />
|
428
|
-
|
429
|
-
<ListBox
|
215
|
+
<ListBox
|
430
|
-
|
431
216
|
Grid.Column="1"
|
432
|
-
|
433
217
|
dd:DragDrop.DropHandler="{Binding}"
|
434
|
-
|
435
218
|
dd:DragDrop.IsDragSource="True"
|
436
|
-
|
437
219
|
dd:DragDrop.IsDropTarget="True"
|
438
|
-
|
439
|
-
DisplayMemberPath="Name"
|
220
|
+
DisplayMemberPath="Name"
|
440
|
-
|
441
221
|
ItemsSource="{Binding UnselectedItems}" />
|
442
|
-
|
443
222
|
</Grid>
|
444
|
-
|
445
223
|
</Window>
|
446
|
-
|
447
|
-
```
|
224
|
+
```
|
448
|
-
|
449
225
|
![イメージ説明](dc019f2128de1fbd553a69bfc6e24fb5.png)
|
450
|
-
|
451
226
|
コードが長くなると本質がわかりにくくなるため、`CollectionViewSource`を使わずシンプルに`SelectedItems` `UnselectedItems`と2つに分けましたが、もちろん使っても構いません。
|