aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/ExtensionMethods/ObservableCollectionExtensions.cs
blob: 59f813281ea97675304b4871a68e58da6a613003 (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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

/// <summary>
/// Contains <see cref="ObservableCollection{T}"/> extension methods.
/// </summary>
public static class ObservableCollectionExtensions
{
    private static object _syncLock = new object();

    /// <summary>
    /// Replaces the specified old element with the specified new element.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="collection">The observable collection.</param>
    /// <param name="oldElement">The old element.</param>
    /// <param name="newElement">The new element.</param>
    public static void Replace<T>(this ObservableCollection<T> collection, T oldElement, T newElement)
    {
        int index = collection.IndexOf(oldElement);
        collection.Remove(oldElement);
        collection.Insert(index, newElement);
    }

    /// <summary>
    /// Swaps the specified elements.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="collection">The observable collection.</param>
    /// <param name="element1">Element1.</param>
    /// <param name="element2">Element2.</param>
    public static void Swap<T>(this ObservableCollection<T> collection, T element1, T element2)
    {
        int index1 = collection.IndexOf(element1);
        int index2 = collection.IndexOf(element2);

        T tmp = collection[index1];
        collection[index1] = collection[index2];
        collection[index2] = tmp;
    }

    /// <summary>
    /// Removes the dragged element and inserts it after the dropped element position.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="collection">The collection.</param>
    /// <param name="dragged">The dragged element.</param>
    /// <param name="dropped">The dropped element.</param>
    public static void DragAndDrop<T>(this ObservableCollection<T> collection, T dragged, T dropped)
    {
        collection.Remove(dragged);
        collection.Insert(collection.IndexOf(dropped), dragged);
    }

    /// <summary>
    /// Enables cross thread operations on this collection.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="collection">The collection.</param>
    public static void EnableCrossThreadOperations<T>(this ObservableCollection<T> collection)
    {
        BindingOperations.EnableCollectionSynchronization(collection, _syncLock);
    }

    /// <summary>
    /// Creates a collection view from the observable collection.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="collection">The collection.</param>
    /// <returns></returns>
    public static ICollectionView ToCollectionView<T>(this ObservableCollection<T> collection)
    {
        return CollectionViewSource.GetDefaultView(collection);
    }
}