Windowscommunitytoolkit: [Question] how to show added children in grouped collection // workaround for ObservableGroup immutable key restriction?

Created on 4 Oct 2020  路  10Comments  路  Source: windows-toolkit/WindowsCommunityToolkit

I'm trying to build an app that allows modifying a grouped collection (nested ObservableCollection) in a ListView. I am showing the grouped collection in the ListView using a CollectionViewSource.
I am not yet using the ObservableGroup / ObservableGroupedCollection yet, my question is whether they are a viable option.

I would like to achieve the following:

  • edit the group names
  • edit the individual items
  • add / remove new groups
  • add / remove individual items

Imagine this (simplified example): having an ObservableCollection<Book>, and every Book has a Title (string) and an ObservableCollection<Uri> for some images.
I want to add books, edit their title, and also add Uris of images and edit them.

  • Inside my ListView, I have

    • a GroupStyle with a HeaderTemplate containing

    • TextBox to edit the Titles

    • a Button to insert new Uri items --> here is my issue

    • an ItemTemplate containing a TextBox for the image Uris

  • outside my ListView I have a Button to add new Book items to the ObservableCollection<Book>

With my current solution (ObservableCollection<Book> with Title and ObservableCollection<Uri> inside Book), I can edit Book Titles and Uris, but when I add a new Uri, the ListView does not show it because the CollectionViewSource does not notice it, because the ObservableCollection<Book> does not emit a CollectionChanged event.

This is being discussed in in this StackOverflow item as well.

Then I found ObservableGroup / ObservableGroupedCollection from Windows Community Toolkit, but I'm unsure whether that will solve my issue? Since the Key is immutable I assume they won't let me edit the Book Titles?

Completed help wanted helpers improvements

All 10 comments

Hello hansmbakker, thank you for your interest in Windows Community Toolkit!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible.. Other community members may also answer the question and provide feedback 馃檶

@hansmbakker that's a good question. Let me loop in the team and see if anyone will be to assist you with this or tell you if it's feasible with ObservableGroup or ObservableGroupCollection.

Thank you!
As a workaround, I see this probably can be built with nested ListViews, or with a custom ItemsRepeater, but I was wondering what the "proper" way of doing this is.
So I'm curious to hear what they will say.

Also, maybe good to mention @vgromfeld who created these types originally :)
(https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/3201)

@hansmbakker ObservableGroup<TKey, TValue> seems to be a good choice since it provides almost all you need.
The only missing part is to make the ObservableGroup<TKey, TValue>.Key property mutable.
This should be easy to change. ObservableGroup<TKey, TValue> is already inheriting from INotifyPropertyChanged so if you change the Key property to read/write, you can directly raise the property changed event and have the behavior you want.

@vgromfeld thank you for your response!

Are you suggesting to modify the classes in the WCT?

If so, I was wondering whether the read-only setting was meant for a particular requirement or behaviour?

@hansmbakker Yes, you can modify the toolkit classes.

The Key property is read-only because I didn't need it to be writable when I implemented it 馃榿.

@hansmbakker thanks for raising the issue, did you also want to submit a PR for this change?

Looking at it in more detail now, I see that the ObservableGroup and ObservableGroupedCollection types are sealed.

I want to base two viewmodel types on them, but because of them being sealed I cannot extend them.

Were there any design considerations to add this restriction?

@michael-hawker I'm providing https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/3526 as-is: I'm willing to contribute the changes that unblocked me, and I updated the documentation for it, but I feel not confident in changing the larger picture of this as I am not aware of design considerations that were there when these classes were added.

For example I left ObservableGroupedCollection sealed. Also, I did not update the IReadOnlyObservableGroup because the name suggests it should be immutable.

I want to avoid getting involved in a long-running PR process / ping-ponging this back and forth between me and others; however maintainers have access to the branch in case they want to make modifications to it.

Was this page helpful?
0 / 5 - 0 ratings