回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,223 +1,112 @@
|
|
1
1
|
> 複数選択情報をプロパティにバインドする方法はないでしょうか?
|
2
2
|
|
3
|
-
|
4
|
-
|
5
3
|
`DataGrid`には`SelectedItems`がありますが、残念ながらバインドできません。
|
6
|
-
|
7
4
|
[MultiSelector.SelectedItems プロパティ (System.Windows.Controls.Primitives) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.multiselector.selecteditems)
|
8
5
|
|
9
|
-
|
10
|
-
|
11
6
|
こちらはどうでしょうか。
|
12
|
-
|
13
7
|
[WPFのDataGridで選択された複数のアイテムをバインドするためのビヘイビア - chorus log](http://chorusde.hatenablog.jp/entry/2013/02/28/064747)
|
14
|
-
|
15
|
-
|
16
8
|
|
17
9
|
`DataGridRow`の`IsSelected`に個々のアイテムのboolをバインドするような方法も見ますが、仮想化が有効だとまともに動きませんので事実上使えません。
|
18
10
|
|
19
|
-
|
20
|
-
|
21
|
-
```x
|
11
|
+
```xml
|
22
|
-
|
23
12
|
<Window
|
24
|
-
|
25
13
|
x:Class="Questions338172.MainWindow"
|
26
|
-
|
27
14
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
28
|
-
|
29
15
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
30
|
-
|
31
16
|
xmlns:Behaviors="http://schemas.microsoft.com/xaml/behaviors"
|
32
|
-
|
33
17
|
xmlns:local="clr-namespace:Questions338172"
|
34
|
-
|
35
18
|
Width="800"
|
36
|
-
|
37
19
|
Height="450">
|
38
|
-
|
39
20
|
<Window.DataContext>
|
40
|
-
|
41
21
|
<local:ViewModel />
|
42
|
-
|
43
22
|
</Window.DataContext>
|
44
|
-
|
45
23
|
<Grid>
|
46
|
-
|
47
24
|
<Grid.ColumnDefinitions>
|
48
|
-
|
49
25
|
<ColumnDefinition />
|
50
|
-
|
51
26
|
<ColumnDefinition />
|
52
|
-
|
53
27
|
</Grid.ColumnDefinitions>
|
54
|
-
|
55
28
|
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items}">
|
56
|
-
|
57
29
|
<Behaviors:Interaction.Behaviors>
|
58
|
-
|
59
30
|
<local:BindSelectedItemsBehavior SelectedItems="{Binding SelectedItems}" />
|
60
|
-
|
61
31
|
</Behaviors:Interaction.Behaviors>
|
62
|
-
|
63
32
|
<DataGrid.Columns>
|
64
|
-
|
65
33
|
<DataGridTextColumn
|
66
|
-
|
67
34
|
Width="*"
|
68
|
-
|
69
35
|
Binding="{Binding Text}"
|
70
|
-
|
71
36
|
Header="Text" />
|
72
|
-
|
73
37
|
</DataGrid.Columns>
|
74
|
-
|
75
38
|
</DataGrid>
|
76
|
-
|
77
39
|
<GroupBox Grid.Column="1" Header="Selected Items">
|
78
|
-
|
79
40
|
<DockPanel>
|
80
|
-
|
81
41
|
<TextBlock DockPanel.Dock="Top" Text="{Binding SelectedItems.Count, StringFormat=Count: {0}}" />
|
82
|
-
|
83
42
|
<ListBox DisplayMemberPath="Text" ItemsSource="{Binding SelectedItems}" />
|
84
|
-
|
85
43
|
</DockPanel>
|
86
|
-
|
87
44
|
</GroupBox>
|
88
|
-
|
89
45
|
</Grid>
|
90
|
-
|
91
46
|
</Window>
|
92
|
-
|
93
47
|
```
|
94
48
|
|
95
|
-
|
96
|
-
|
97
|
-
```
|
49
|
+
```cs
|
98
|
-
|
99
50
|
using CommunityToolkit.Mvvm.ComponentModel;
|
100
|
-
|
101
51
|
using Microsoft.Xaml.Behaviors;
|
102
|
-
|
103
52
|
using System.Collections;
|
104
|
-
|
105
53
|
using System.Collections.ObjectModel;
|
106
|
-
|
107
54
|
using System.Linq;
|
108
|
-
|
109
55
|
using System.Windows;
|
110
|
-
|
111
56
|
using System.Windows.Controls;
|
112
|
-
|
113
57
|
using System.Windows.Data;
|
114
58
|
|
115
|
-
|
116
|
-
|
117
59
|
namespace Questions338172
|
118
|
-
|
119
60
|
{
|
120
|
-
|
121
61
|
// [WPFのDataGridで選択された複数のアイテムをバインドするためのビヘイビア - chorus log](http://chorusde.hatenablog.jp/entry/2013/02/28/064747)
|
122
|
-
|
123
62
|
class BindSelectedItemsBehavior : Behavior<DataGrid>
|
124
|
-
|
125
63
|
{
|
126
|
-
|
127
64
|
public static DependencyProperty SelectedItemsProperty
|
128
|
-
|
129
65
|
= DependencyProperty.Register(nameof(SelectedItems), typeof(IList), typeof(BindSelectedItemsBehavior),
|
130
|
-
|
131
66
|
new PropertyMetadata(null));
|
132
|
-
|
133
67
|
public IList SelectedItems { get => (IList)GetValue(SelectedItemsProperty); set => SetValue(SelectedItemsProperty, value); }
|
134
68
|
|
135
|
-
|
136
|
-
|
137
69
|
protected override void OnAttached() => AssociatedObject.SelectionChanged += SelectionChanged;
|
138
|
-
|
139
70
|
protected override void OnDetaching() => AssociatedObject.SelectionChanged -= SelectionChanged;
|
140
71
|
|
141
|
-
|
142
|
-
|
143
72
|
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
|
144
|
-
|
145
73
|
{
|
146
|
-
|
147
74
|
if (SelectedItems == null) return;
|
148
75
|
|
149
|
-
|
150
|
-
|
151
76
|
foreach (var addedItem in e.AddedItems)
|
152
|
-
|
153
77
|
{
|
154
|
-
|
155
78
|
if (addedItem == CollectionView.NewItemPlaceholder) continue;
|
156
|
-
|
157
79
|
SelectedItems.Add(addedItem);
|
158
|
-
|
159
80
|
}
|
160
81
|
|
161
|
-
|
162
|
-
|
163
82
|
foreach (var removedItem in e.RemovedItems)
|
164
|
-
|
165
83
|
SelectedItems.Remove(removedItem);
|
166
|
-
|
167
84
|
}
|
168
|
-
|
169
85
|
}
|
170
86
|
|
171
|
-
|
172
|
-
|
173
87
|
class Item
|
174
|
-
|
175
88
|
{
|
176
|
-
|
177
89
|
public string Text { get; set; }
|
178
|
-
|
179
90
|
}
|
180
91
|
|
181
|
-
|
182
|
-
|
183
92
|
class ViewModel
|
184
|
-
|
185
93
|
{
|
186
|
-
|
187
94
|
public ObservableCollection<Item> Items { get; }
|
188
|
-
|
189
95
|
public ObservableCollection<Item> SelectedItems { get; }
|
190
96
|
|
191
|
-
|
192
|
-
|
193
97
|
public ViewModel()
|
194
|
-
|
195
98
|
{
|
196
|
-
|
197
99
|
Items = new ObservableCollection<Item>(Enumerable.Range(0, 1_000).Select(x => new Item { Text = $"Item{x}" }));
|
198
|
-
|
199
100
|
SelectedItems = new ObservableCollection<Item>();
|
200
|
-
|
201
101
|
}
|
202
|
-
|
203
102
|
}
|
204
103
|
|
205
|
-
|
206
|
-
|
207
104
|
public partial class MainWindow : Window
|
208
|
-
|
209
105
|
{
|
210
|
-
|
211
106
|
public MainWindow() => InitializeComponent();
|
212
|
-
|
213
107
|
}
|
214
|
-
|
215
108
|
}
|
216
|
-
|
217
109
|
```
|
218
110
|
|
219
|
-
|
220
|
-
|
221
111
|
こちらを使用しています。
|
222
|
-
|
223
112
|
[NuGet Gallery | Microsoft.Xaml.Behaviors.Wpf 1.1.31](https://www.nuget.org/packages/Microsoft.Xaml.Behaviors.Wpf/1.1.31)
|