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
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.SharedUI.Components;
public static class ISelectedObjectCollectionExt
{
public static IList<SelectedObject> GetItems(this ISelectedObjectCollection source)
{
return source.Cast<SelectedObject>().ToList();
}
}
namespace Tango.SharedUI.Components
{
public interface ISelectedObjectCollection : IEnumerable, INotifyCollectionChanged
{
event EventHandler SelectionChanged;
ObservableCollection<Object> Source { get; set; }
ObservableCollection<Object> SynchedSource { get; set; }
}
public class SelectedObjectCollection<T> : ObservableCollection<SelectedObject<T>>, ISelectedObjectCollection
{
private Func<T, T, bool> _compareFunc;
public event EventHandler SelectionChanged;
public ObservableCollection<T> Source { get; set; }
public ObservableCollection<T> SynchedSource { get; set; }
ObservableCollection<object> ISelectedObjectCollection.Source { get; set; }
ObservableCollection<object> ISelectedObjectCollection.SynchedSource { get; set; }
public bool IsReadOnly { get; }
public SelectedObjectCollection(ObservableCollection<T> source, ObservableCollection<T> synchedSource, Func<T, T, bool> compareFunc)
{
_compareFunc = compareFunc;
SynchedSource = synchedSource;
Source = source;
foreach (var item in source)
{
var selectedItem = new SelectedObject<T>(item, _compareFunc == null ? synchedSource.Contains(item) : synchedSource.ToList().Exists(x => _compareFunc(x, item)));
this.Add(selectedItem);
selectedItem.IsSelectedChanged += SelectedItem_IsSelectedChanged;
}
}
public SelectedObjectCollection(ObservableCollection<T> source, ObservableCollection<T> synchedSource) : this(source, synchedSource, null)
{
}
private void SelectedItem_IsSelectedChanged(object sender, EventArgs e)
{
SelectedObject<T> item = sender as SelectedObject<T>;
if (item.IsSelected)
{
if (_compareFunc == null)
{
if (!SynchedSource.Contains(item.Data))
{
SynchedSource.Add(item.Data);
}
}
else
{
if (!SynchedSource.ToList().Exists(x => _compareFunc(x, item.Data)))
{
SynchedSource.Add(item.Data);
}
}
}
else
{
if (_compareFunc == null)
{
SynchedSource.Remove(item.Data);
}
else
{
foreach (var s in SynchedSource.ToList())
{
if (_compareFunc(s, item.Data))
{
SynchedSource.Remove(s);
}
}
}
}
SelectionChanged?.Invoke(this, new EventArgs());
}
public void Add(SelectedObject item)
{
base.Add((SelectedObject<T>)item);
}
public bool Contains(SelectedObject item)
{
return base.Contains((SelectedObject<T>)item);
}
public void CopyTo(SelectedObject[] array, int arrayIndex)
{
base.CopyTo(array.Cast<SelectedObject<T>>().ToArray(), arrayIndex);
}
public bool Remove(SelectedObject item)
{
return base.Remove((SelectedObject<T>)item);
}
}
}
|