forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservableGroup.cs
More file actions
80 lines (71 loc) · 2.76 KB
/
ObservableGroup.cs
File metadata and controls
80 lines (71 loc) · 2.76 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
namespace Microsoft.Toolkit.Collections
{
/// <summary>
/// An observable group.
/// It associates a <see cref="Key"/> to an <see cref="ObservableCollection{T}"/>.
/// </summary>
/// <typeparam name="TKey">The type of the group key.</typeparam>
/// <typeparam name="TValue">The type of the items in the collection.</typeparam>
[DebuggerDisplay("Key = {Key}, Count = {Count}")]
public class ObservableGroup<TKey, TValue> : ObservableCollection<TValue>, IGrouping<TKey, TValue>, IReadOnlyObservableGroup
where TKey : notnull
{
/// <summary>
/// The cached <see cref="PropertyChangedEventArgs"/> for <see cref="Key"/>
/// </summary>
private static readonly PropertyChangedEventArgs KeyChangedEventArgs = new PropertyChangedEventArgs(nameof(Key));
/// <summary>
/// Initializes a new instance of the <see cref="ObservableGroup{TKey, TValue}"/> class.
/// </summary>
/// <param name="key">The key for the group.</param>
public ObservableGroup(TKey key)
{
this.key = key;
}
/// <summary>
/// Initializes a new instance of the <see cref="ObservableGroup{TKey, TValue}"/> class.
/// </summary>
/// <param name="grouping">The grouping to fill the group.</param>
public ObservableGroup(IGrouping<TKey, TValue> grouping)
: base(grouping)
{
this.key = grouping.Key;
}
/// <summary>
/// Initializes a new instance of the <see cref="ObservableGroup{TKey, TValue}"/> class.
/// </summary>
/// <param name="key">The key for the group.</param>
/// <param name="collection">The initial collection of data to add to the group.</param>
public ObservableGroup(TKey key, IEnumerable<TValue> collection)
: base(collection)
{
this.key = key;
}
private TKey key;
/// <summary>
/// Gets or sets the key of the group.
/// </summary>
public TKey Key
{
get => this.key;
set
{
if (!EqualityComparer<TKey>.Default.Equals(this.key!, value))
{
this.key = value;
OnPropertyChanged(KeyChangedEventArgs);
}
}
}
/// <inheritdoc/>
object IReadOnlyObservableGroup.Key => Key;
}
}