locked
Collectionviewsource and observablecollections WP 8.1 RRS feed

  • Question

  • Hello,

    I'm not sure how to go about using a collectionviewsource with WP 8.1 and listview.

    I'm using the MVVM-pattern with all code inside CatViewModel.xaml.cs, and all xaml inside app.xaml.

    I have a model which is a set of cats that's inside a database where I fetch data with sqlite.

    Now obviously I want to return this observablecollection to my app.xaml, but I'm getting nothing in there. If i put a breakpoint on my ICollectionView get-statement it never gets there. Items is never called.

    app.xaml

    Datatemplate of my listview

    <Grid.Resources>
     <CollectionViewSource x:Name="Items" IsSourceGrouped="True"/>
    </Grid.Resources>


    <ListView ItemsSource="{Binding Source={StaticResource Items}}"
                                  IsItemClickEnabled="False" ItemTemplate="{StaticResource CatIT}">


    itemtemplate CatIT

    <ListViewItem>
        <TextBlock Text="{Binding Cat}" Grid.Row="0" Grid.Column="0" Style="{StaticResource SmallFont}" />
    </ListViewItem>
        

    CatViewModel.xaml.cs (This code below is a rip from Alexanders code)
    http://social.msdn.microsoft.com/Forums/silverlight/en-US/29699048-3ecb-407e-a28c-cd826024fbb2/using-collectionviewsource-in-mvvm?forum=silverlightcontrols

    private CollectionViewSource items;
    public ICollectionView Items
    {
    	get
    	{
    		if (items == null)
    		{
    			items = new CollectionViewSource();
                            //where listOfDataObjects is collection of your data objects
                            //that you want to display
    			items.Source = new ObservableCollection<DataObject>(listOfDataObjects);
    
    		}
    
    		return items.View;
    	}
    }


    Tuesday, July 15, 2014 6:24 AM

All replies

  • Not sure what the issue could be from what you have posted.

    Please post a sample project showing the problem on OneDrive


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, July 16, 2014 12:18 PM
  • Sorry Jeff about the delay. I have been away for one day.

    I created a new project and came up with a simpler version but now what's displayed is only the print screen. "Utmaningen" is my Project, .model is my folder where I have my models.

    This is a new version of my code. I am trying to keep it to the absolute simplest possible but out of all code models, going from knowing nothing of coding to learning MVVM, async-tasks, WCF in azure, Sqlite, and local databases to make my app all in C# in about one year I think this is the hardest to wrap my head around. :) I read many articles online and all but they are all so conflicting. And many includes the longlistselector.

    App.xaml

      <ListView ItemsSource="{Binding Path=CatSorted}" >
                                                  <ListView.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Key}" Foreground="White" />
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>
                            </GroupStyle>
                        </ListView.GroupStyle>
    </Listview>

    Folder viewmodel

    catviewmodel.xaml.cs

    private CollectionViewSource catSorted; public ICollectionView CatSorted { get { if (catSorted == null) { catSorted = new CollectionViewSource(); var GroupedCatsByDate = from c in cats.GetCats()

    group c by e.CatDate into groupedByDate select new KeyedList<string, CatModel>(groupedByDate); catSorted.Source = new ObservableCollection<KeyedList<string, CatModel>>(grouedByDate); } return catSorted.View; } }



    Friday, July 18, 2014 6:03 AM
  • Does anyone have an idea about this problem?
    Saturday, July 19, 2014 10:46 AM
  • I'll try once more othervise I'll close the thread. Anyone?
    Wednesday, July 23, 2014 6:45 PM
  • Please post a simple repro project to onedrive showing the issue and you attempt to resolve it. 

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, July 23, 2014 6:53 PM
  • Did anyone else have an idea about this? Without having to upload my entire app?
    Tuesday, September 30, 2014 8:56 PM