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 GetItems(this ISelectedObjectCollection source) { return source.Cast().ToList(); } } namespace Tango.SharedUI.Components { public interface ISelectedObjectCollection : IEnumerable, INotifyCollectionChanged { event EventHandler SelectionChanged; ObservableCollection Source { get; set; } ObservableCollection SynchedSource { get; set; } } public class SelectedObjectCollection : ObservableCollection>, ISelectedObjectCollection { private Func _compareFunc; public event EventHandler SelectionChanged; public ObservableCollection Source { get; set; } public ObservableCollection SynchedSource { get; set; } ObservableCollection ISelectedObjectCollection.Source { get; set; } ObservableCollection ISelectedObjectCollection.SynchedSource { get; set; } public bool IsReadOnly { get; } public SelectedObjectCollection(ObservableCollection source, ObservableCollection synchedSource, Func compareFunc) { _compareFunc = compareFunc; SynchedSource = synchedSource; Source = source; foreach (var item in source) { var selectedItem = new SelectedObject(item, _compareFunc == null ? synchedSource.Contains(item) : synchedSource.ToList().Exists(x => _compareFunc(x, item))); this.Add(selectedItem); selectedItem.IsSelectedChanged += SelectedItem_IsSelectedChanged; } } public SelectedObjectCollection(ObservableCollection source, ObservableCollection synchedSource) : this(source, synchedSource, null) { } private void SelectedItem_IsSelectedChanged(object sender, EventArgs e) { SelectedObject item = sender as SelectedObject; 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)item); } public bool Contains(SelectedObject item) { return base.Contains((SelectedObject)item); } public void CopyTo(SelectedObject[] array, int arrayIndex) { base.CopyTo(array.Cast>().ToArray(), arrayIndex); } public bool Remove(SelectedObject item) { return base.Remove((SelectedObject)item); } } }