2
// ChildrenCollection.cs
5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.ObjectModel;
29
using System.Collections.Generic;
33
public class ChildrenCollection<T>: Collection<T>
35
ICollectionEventSink<T> eventSink;
37
public ChildrenCollection (ICollectionEventSink<T> eventSink)
39
this.eventSink = eventSink;
42
protected override void InsertItem (int index, T item)
44
base.InsertItem (index, item);
45
eventSink.AddedItem (item, index);
48
protected override void RemoveItem (int index)
51
base.RemoveItem (index);
52
eventSink.RemovedItem (item, index);
55
protected override void SetItem (int index, T item)
57
T oldItem = this[index];
58
base.SetItem (index, item);
59
eventSink.RemovedItem (oldItem, index);
60
eventSink.AddedItem (item, index);
63
protected override void ClearItems ()
65
List<T> items = new List<T> (this);
67
for (int n=0; n<items.Count; n++)
68
eventSink.RemovedItem (items[n], n);
72
public interface ICollectionEventSink<T>
74
void AddedItem (T item, int index);
75
void RemovedItem (T item, int index);