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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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());
        }
    }
}
n> e.NewValue != null) { if (!(d is TextBox)) { throw new ArgumentException("The text controller component can only handle elements of type TextBox."); } (e.NewValue as TextController).SetTextBox(d as TextBox); } } /// <summary> /// Sets the Controller attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">if set to <c>true</c> [value].</param> public static void SetController(FrameworkElement element, TextController value) { element.SetValue(ControllerProperty, value); } /// <summary> /// Gets the Controller attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static TextController GetController(FrameworkElement element) { return (TextController)element.GetValue(ControllerProperty); } /// <summary> /// Gets or sets a value indicating whether to automatically scroll to end of text. /// </summary> public bool AutoScrollToEnd { get; set; } /// <summary> /// Initializes a new instance of the <see cref="TextController"/> class. /// </summary> public TextController() { AutoScrollToEnd = true; } /// <summary> /// Sets the text box. /// </summary> /// <param name="textBox">The text box.</param> private void SetTextBox(TextBox textBox) { _textBox = textBox; _textBox.PreviewMouseDown += (_, __) => _isMouseDown = true; _textBox.PreviewMouseUp += (_, __) => _isMouseDown = false; } /// <summary> /// Appends the specified text. /// </summary> /// <param name="text">The text.</param> public void WriteLine(String text) { Write(text + Environment.NewLine); } /// <summary> /// Appends the specified text. /// </summary> /// <param name="text">The text.</param> public void Write(String text) { Invoke(() => { _textBox.AppendText(text); if (AutoScrollToEnd && !_isMouseDown) { ScrollToEnd(); } }); } /// <summary> /// Sets the specified text. /// </summary> /// <param name="text">The text.</param> public void Set(String text) { Invoke(() => { _textBox.Text = text; if (AutoScrollToEnd && !_isMouseDown) { ScrollToEnd(); } }); } /// <summary> /// Clears the text. /// </summary> public void Clear() { Invoke(() => { _textBox.Clear(); }); } /// <summary> /// Scrolls to the end of the text. /// </summary> public void ScrollToEnd() { _textBox.ScrollToEnd(); } /// <summary> /// Invokes the specified action. /// </summary> /// <param name="action">The action.</param> private void Invoke(Action action) { Dispatcher.BeginInvoke(action); } } }