aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/Components/SelectedObjectCollection.cs
blob: e09bb832e638f9b89df82884227b6d3daa0164bd (plain)
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
32pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { c
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.SharedUI.Components
{
    public class SelectedObjectCollection<T> : ObservableCollection<SelectedObject<T>>
    {
        public event EventHandler SelectionChanged;
        public ObservableCollection<T> Source { get; set; }
        public ObservableCollection<T> SynchedSource { get; set; }

        public SelectedObjectCollection(ObservableCollection<T> source, ObservableCollection<T> synchedSource)
        {
            SynchedSource = synchedSource;
            Source = source;

            foreach (var item in source)
            {
                var selectedItem = new SelectedObject<T>(item, synchedSource.Contains(item));
                this.Add(selectedItem);
                selectedItem.IsSelectedChanged += SelectedItem_IsSelectedChanged;
            }
        }

        private void SelectedItem_IsSelectedChanged(object sender, EventArgs e)
        {
            SelectedObject<T> item = sender as SelectedObject<T>;
            
            if (item.IsSelected)
            {
                if (!SynchedSource.Contains(item.Data))
                {
                    SynchedSource.Add(item.Data);
                }
            }
            else
            {
                SynchedSource.Remove(item.Data);
            }

            SelectionChanged?.Invoke(this, new EventArgs());
        }
    }
}