Skip to content

Commit 830e9af

Browse files
author
msftbot[bot]
authored
Mutable and subclassable version of ObservableGroup (#3526)
## Fixes #3519 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> Removes two restrictions from `ObservableGroup` to make it usable in more situations. In my case, I wanted to be able to rename the groups (for this I needed a mutable key), and I wanted to add properties on the group level (I needed to unseal it) - Removes the immutable restriction on the `Key` property and makes the `Key` property observable. - Removes the `sealed` keyword from `ObservableGroup` ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> <!-- - Bugfix --> - Feature <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> `ObservableGroup` has an immutable `Key` property `ObservableGroup` is not inheritable ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> `ObservableGroup` has a mutable `Key` property `ObservableGroup` is inheritable ## PR Checklist Please check if your PR fulfills the following requirements: - [ ] Tested code with current [supported SDKs](../readme.md#supported) - [x] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: MicrosoftDocs/WindowsCommunityToolkitDocs#394 - [x] Sample in sample app has been added / updated (for bug fixes / features) --> _no update needed_ - [x] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets) --> _not applicable_ - [x] Tests for the changes have been added (for bug fixes / features) (if applicable) --> _not applicable_ - [x] Header has been added to all new source files (run *build/UpdateHeaders.bat*) --> _not applicable_ - [ ] Contains **NO** breaking changes <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. Please note that breaking changes are likely to be rejected. --> ## Other information
2 parents 6583c81 + b601e57 commit 830e9af

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

Microsoft.Toolkit/Collections/ObservableGroup.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
7+
using System.ComponentModel;
78
using System.Diagnostics;
89
using System.Linq;
910

@@ -16,8 +17,13 @@ namespace Microsoft.Toolkit.Collections
1617
/// <typeparam name="TKey">The type of the group key.</typeparam>
1718
/// <typeparam name="TValue">The type of the items in the collection.</typeparam>
1819
[DebuggerDisplay("Key = {Key}, Count = {Count}")]
19-
public sealed class ObservableGroup<TKey, TValue> : ObservableCollection<TValue>, IGrouping<TKey, TValue>, IReadOnlyObservableGroup
20+
public class ObservableGroup<TKey, TValue> : ObservableCollection<TValue>, IGrouping<TKey, TValue>, IReadOnlyObservableGroup
2021
{
22+
/// <summary>
23+
/// The cached <see cref="PropertyChangedEventArgs"/> for <see cref="Key"/>
24+
/// </summary>
25+
private static readonly PropertyChangedEventArgs KeyChangedEventArgs = new PropertyChangedEventArgs(nameof(Key));
26+
2127
/// <summary>
2228
/// Initializes a new instance of the <see cref="ObservableGroup{TKey, TValue}"/> class.
2329
/// </summary>
@@ -48,10 +54,24 @@ public ObservableGroup(TKey key, IEnumerable<TValue> collection)
4854
Key = key;
4955
}
5056

57+
private TKey key;
58+
5159
/// <summary>
52-
/// Gets the key of the group.
60+
/// Gets or sets the key of the group.
5361
/// </summary>
54-
public TKey Key { get; }
62+
public TKey Key
63+
{
64+
get => this.key;
65+
set
66+
{
67+
if (!EqualityComparer<TKey>.Default.Equals(this.key, value))
68+
{
69+
this.key = value;
70+
71+
OnPropertyChanged(KeyChangedEventArgs);
72+
}
73+
}
74+
}
5575

5676
/// <inheritdoc/>
5777
object IReadOnlyObservableGroup.Key => Key;

0 commit comments

Comments
 (0)