GroupedObservableCollection
GroupedObservableCollection copied to clipboard
Error on comiling
Hi Mike,
i tried to implement and use your GroupedObservableCollection, but on compiling i got the following error
Cannot determine the item type of collection type 'MangaReader_MVVM.Utils.GroupedObservableCollection'2[System.char,MangaReader_MVVM.Models.Manga]' because it has more than one Add method or ICollection<T> implementation. To make this collection type usable in XAML, add a public Add(object) method, implement System.Collections.IList or a single System.Collections.Generic.ICollection<T>.
and i don't know where this other Add method should be.
I didn't change anything of your code, just used it...so...do you maybe have an idea?
It sounds like you're embedding a GroupedObservableCollection directly in XAML, rather than binding one to a CollectionViewSource? Could you post the XAML that's giving the error?
Hm...actually i tried to add it from code Behind
//Favorites is an ObservableCollection<Manga>
public GroupedObservableCollection<char, Manga> FavoritesGroups = new GroupedObservableCollection<char, Manga>(m => m.Title[0], Favorites);
private CollectionViewSource _favoritesCVS;
public CollectionViewSource FavoritesCVS
{
get { return _favoritesCVS = _favoritesCVS ?? new CollectionViewSource() { IsSourceGrouped = true, Source = FavoritesGroups }; }
set { Set(ref _favoritesCVS, value); }
}
in XAML i'm Binding it like this
<GridView x:Name="ZoomedInGridView"
ItemsSource="{x:Bind ViewModel.FavoritsCVS.View, Mode=OneWay}"
...more stuff... >
<GridView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate x:DataType="utils:Grouping">
<TextBlock Text="{x:Bind Key}" Margin="0,0,0,-10"
Style="{ThemeResource SubheaderTextBlockStyle}"
Foreground="{ThemeResource SystemControlHighlightAccentBrush}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
I hope this helps. Do you also need the "Manga"-Model?
Hi, sorry for the delay - it looks like you're binding your ListView
to ViewModel.FavoritsCVS.View
- I think you should be binding it to the CollectionViewSource
itself, e.g. ViewModel.FavoritsCVS
.
Does that help?
Got it working, thank you for the help. I think the problem was some kind of build artifact in the build folder.
ok, i was wrong. it's still there. when i get the GroupedObservableCollection from a Helper/Service as a Property and use that Property as a Source for the CVS i get the error. The same code as above:
public GroupedObservableCollection<char, Manga> FavoritesGroups { get { return _library.Favorites; } }
private CollectionViewSource _favoritesCVS;
public CollectionViewSource FavoritesCVS
{
get { return _favoritesCVS = _favoritesCVS ?? new CollectionViewSource() { IsSourceGrouped = true, Source = FavoritesGroups }; }
set { Set(ref _favoritesCVS, value); }
}
when i change the code like this
private CollectionViewSource _favoritesCVS;
public CollectionViewSource FavoritesCVS
{
get { return _favoritesCVS = _favoritesCVS ?? new CollectionViewSource() { IsSourceGrouped = true, Source = new GroupedObservableCollection<char,Manga>(m => m.Title[0], Favorites) }; }
set { Set(ref _favoritesCVS, value); }
}
//Favorites is an ObservableCollection<Manga>
it works. Do you have any idea why that is?