CollectionViewSource "The value is not in the expected range."

Why does this code cause an error in a Windows 8 XAML application?

The value does not fall within the expected range.

XAML:

<SemanticZoom> <SemanticZoom.ZoomedInView> <ListView Style="{StaticResource HorizontalListViewStyle}" SelectionMode="None" ScrollViewer.IsHorizontalScrollChainingEnabled="False" ItemsSource="{Binding BoardItems}" ItemContainerStyle="{StaticResource ZoomedOutListViewItemContainerStyle}" ... 

MVVM Code:

 ObservableCollection<WritingBoardModel> boards = new ObservableCollection<WritingBoardModel>(); ... // Add item models to boards. CollectionViewSource v = new CollectionViewSource() { Source = boards, }; this.ViewModel.Add(BoardItemsViewModelKey, v); 

If I skip the CollectionViewSource and directly add the boards instance to my view model, then all of this will work.

I think I need to use CollectionViewSource in order to get some semantic zooming to work.

+5
source share
3 answers

I need to associate with the View property of a CollectionViewSource as follows:

 CollectionViewSource v = new CollectionViewSource() { IsSourceGrouped = false, Source = boards, }; this.ViewModel.Add(BoardItemsViewModelKey, v.View); 

Remember that this does not help my two ListView and keeps them in sync of choice in SemanticZoom .

0
source

So, CollectionViewSource weird, and the way to bind to them is weird too. To give you an example to do this โ€œfrom the bookโ€ (as sample projects do), I found that this should practically be a StaticResource as such:

 <Page.Resource> <CollectionViewSource Source="{Binding Whatev}" x:Key="WhatevSource"/> </Page.Resource> <GridView ItemsSource="{Binding Source={StaticResource WhatevSource}}"/> 

Please note that we do not put the source directly to the CollectionViewSource , but we set the โ€œimpenetrableโ€ Binding , mainly using the CollectionViewSource as a DataContext (only one way to think about it, and not actually technically correct).

This is the only way I was able to make it work, although I believe that technically you can set the ItemsSource directly in the View of CollectionViewSource or something similar in the given code.

+5
source

In your Add List, StaticResource and Source

 <ListView ItemsSource="{Binding Source={StaticResource WhatevSource}}"/> 
+2
source

Source: https://habr.com/ru/post/1491704/


All Articles