回答編集履歴
1
Indexプロパティを使用した方法を追記
test
CHANGED
@@ -159,3 +159,125 @@
|
|
159
159
|
</ListBox>
|
160
160
|
|
161
161
|
```
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
----
|
166
|
+
|
167
|
+
**(追記)**
|
168
|
+
|
169
|
+
データにIndexプロパティを持たせられるのであれば、ValueConverterも使用しない素直な実装になります。
|
170
|
+
|
171
|
+
以下の例では項目変更時に全ての`Index`を更新するようにしていますが、項目を入れ替える時にその項目の`Index`も入れ替える、という方法もあります。
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
```C#
|
176
|
+
|
177
|
+
public partial class MainWindow : Window
|
178
|
+
|
179
|
+
{
|
180
|
+
|
181
|
+
public ObservableCollection<MyItem> Collection { get; } = new ObservableCollection<MyItem>()
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
new MyItem{ ItemName="item1" },
|
186
|
+
|
187
|
+
new MyItem{ ItemName="item2" },
|
188
|
+
|
189
|
+
new MyItem{ ItemName="item3" },
|
190
|
+
|
191
|
+
new MyItem{ ItemName="item4" },
|
192
|
+
|
193
|
+
new MyItem{ ItemName="item5" }
|
194
|
+
|
195
|
+
};
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
public MainWindow()
|
200
|
+
|
201
|
+
{
|
202
|
+
|
203
|
+
InitializeComponent();
|
204
|
+
|
205
|
+
this.DataContext = this;
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
RefleshCollectionIndex();
|
210
|
+
|
211
|
+
this.Collection.CollectionChanged += (s, e) => RefleshCollectionIndex();
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
public void RefleshCollectionIndex()
|
218
|
+
|
219
|
+
{
|
220
|
+
|
221
|
+
for (int i = 0; i < this.Collection.Count; ++i)
|
222
|
+
|
223
|
+
{
|
224
|
+
|
225
|
+
this.Collection[i].Index = i;
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
//
|
236
|
+
|
237
|
+
public class MyItem : INotifyPropertyChanged
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
public event PropertyChangedEventHandler PropertyChanged;
|
242
|
+
|
243
|
+
protected void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name = null)
|
244
|
+
|
245
|
+
{
|
246
|
+
|
247
|
+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
private int _index;
|
254
|
+
|
255
|
+
public int Index
|
256
|
+
|
257
|
+
{
|
258
|
+
|
259
|
+
get { return _index; }
|
260
|
+
|
261
|
+
set { if (_index != value) { _index = value; RaisePropertyChanged(); } }
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
private string _itemName;
|
268
|
+
|
269
|
+
public string ItemName
|
270
|
+
|
271
|
+
{
|
272
|
+
|
273
|
+
get { return _itemName; }
|
274
|
+
|
275
|
+
set { if (_itemName != value) { _itemName = value; RaisePropertyChanged(); } }
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
```
|