/
ObservableCollectionListAdapter.cs
178 lines (166 loc) · 6.33 KB
/
ObservableCollectionListAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace System.Windows.Controls.DataVisualization
{
/// <summary>
/// An object that synchronizes changes in an observable collection to
/// a list.
/// </summary>
/// <typeparam name="T">The type of the objects in the collection.
/// </typeparam>
internal class ObservableCollectionListAdapter<T>
where T : class
{
/// <summary>
/// The collection to synchronize with a list.
/// </summary>
private IEnumerable _collection;
/// <summary>
/// Gets or sets the collection to synchronize with a list.
/// </summary>
public IEnumerable Collection
{
get
{
return _collection;
}
set
{
IEnumerable oldValue = _collection;
INotifyCollectionChanged oldObservableCollection = oldValue as INotifyCollectionChanged;
INotifyCollectionChanged newObservableCollection = value as INotifyCollectionChanged;
_collection = value;
if (oldObservableCollection != null)
{
oldObservableCollection.CollectionChanged -= OnCollectionChanged;
}
if (value == null && TargetList != null)
{
TargetList.Clear();
}
if (newObservableCollection != null)
{
newObservableCollection.CollectionChanged += OnCollectionChanged;
}
}
}
/// <summary>
/// Gets or sets the panel to synchronize with the collection.
/// </summary>
public IList TargetList { get; set; }
/// <summary>
/// Method that synchronizes the panel's child collection with the
/// contents of the observable collection when it changes.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">Information about the event.</param>
public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (TargetList != null)
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
TargetList.Clear();
}
else if (e.Action == NotifyCollectionChangedAction.Replace)
{
for (int cnt = 0; cnt < e.OldItems.Count; cnt++)
{
T oldItem = e.OldItems[cnt] as T;
T newItem = e.NewItems[cnt] as T;
int index = TargetList.IndexOf(oldItem);
if (index != -1)
{
TargetList[index] = newItem;
}
else
{
TargetList.Remove(oldItem);
TargetList.Add(newItem);
}
}
}
else
{
if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems != null)
{
foreach (T element in e.OldItems)
{
TargetList.Remove(element);
}
}
else if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null)
{
int startingIndex = e.NewStartingIndex;
if (startingIndex != 0)
{
T previousElement = Collection.FastElementAt<T>(startingIndex - 1);
int targetListIndex = TargetList.IndexOf(previousElement);
if (targetListIndex != -1)
{
startingIndex = targetListIndex + 1;
}
}
else if (Collection.FastCount() > 1)
{
T nextElement = Collection.FastElementAt<T>(startingIndex + 1);
int targetListIndex = TargetList.IndexOf(nextElement);
if (targetListIndex == -1)
{
startingIndex = 0;
}
else
{
startingIndex = targetListIndex;
}
}
e.NewItems
.OfType<T>()
.ForEachWithIndex((item, index) =>
{
TargetList.Insert(startingIndex + index, item);
});
}
}
}
}
/// <summary>
/// A method that populates a panel with the items in the collection.
/// </summary>
public void Populate()
{
if (TargetList != null)
{
if (Collection != null)
{
foreach (T item in Collection)
{
TargetList.Add(item);
}
}
else
{
TargetList.Clear();
}
}
}
/// <summary>
/// Removes the items in the adapted list from the target list.
/// </summary>
public void ClearItems()
{
foreach (T item in this.Collection)
{
this.TargetList.Remove(item);
}
}
}
}