问题
I have the following two classes for a RageComic reader metro style app I am making.
public class ComicDataWNGroup
{
public ComicDataWNGroup(string title)
{
this.Title = title;
this.Items = new ObservableCollection<ComicDataItem>();
}
public String Title { get; set; }
public ObservableCollection<ComicDataItem> Items { get; private set; }
}
public class ComicDataWNSource
{
private RedditAPI.RedditManager currentManager;
public ComicDataWNSource(RedditAPI.RedditManager currentManager)
{
this.currentManager = currentManager;
Groups = new ObservableCollection<ComicDataWNGroup>();
}
public async Task<int> ObtainRecentContent()
{
SubredditComicDataSource comicList = new SubredditComicDataSource();
await comicList.LoadItems();
var comicSources = comicList.GetAllItems();
for (int i = 0; i < comicSources.Count; i++ )
{
var links = await this.currentManager.GetSubredditLinks(comicSources[i].UrlTitle, 6, 0);
ComicDataWNGroup group = new ComicDataWNGroup("From " + comicSources[i].Title);
for(int j = 0; j < links.Data.Count; j++)
{
var linkItem = (RedditAPI.Objects.Link)links.Data[j];
BitmapImage img = await ThumbnailRetriever.GetThumbnailFromRedditUrl(linkItem.ThumbnailUrl);
ComicDataItem item = new ComicDataItem(linkItem.Title, false, true, 0, img, 0, null, null, Visibility.Visible, Visibility.Collapsed);
group.Items.Add(item);
}
Groups.Add(group);
}
return 0;
}
public ObservableCollection<ComicDataWNGroup> Groups
{
{ get; private set; }
}
}
Note: ComicDataItem is a class that mostly contains get set properties. I can post it later if it is needed.
Using these two classes, I am trying to bind the Groups property of the ComicDataWNSource object to a GridView.
The following code is the code I use for the binding.
DataModel.ComicDataWNSource source = new DataModel.ComicDataWNSource(App.Manager);
await source.ObtainRecentContent();
CollectionViewSource viewSource = new CollectionViewSource();
viewSource.Source = source.Groups;
viewSource.IsSourceGrouped = true;
viewSource.ItemsPath = new PropertyPath("Items");
this.itemGridView.ItemsSource = viewSource;
The GridView is the default one generated by Visual Studio when creating a new Grouped Items Page. I already changed the appropriate bindings to match up with the properties in the ComicDataWNGroup class and ComicDataItem class (I can post the XAML for the GridView as well if needed). Also, source.ObtainRecentContent() is currently filling up the Groups array with the correct data. The array of Groups and inner array of Items both have objects in them and neither of them are null.
The problem arises on the line setting the view source to the grid view. Whenever I try it, it throws the following exception:
Argument Exception: Value does not fall within the expected range.
I don't have a clue what I could be doing wrong for this to happen. I already looked at this msdn page already, which shows you how to use a grouped grid view item and view source. Any ideas what I'm doing wrong?
回答1:
Ok, I managed to make this work by doing the following:
In the xaml where my GridView is defined, I declared the CollectionViewSource in the same xaml file instead of the code behind file.
<Page.Resources>
<CollectionViewSource x:Name="itemsViewSource" ></CollectionViewSource>
</Page.Resources>
I bound the ItemsSource property of the GridView with the following property:
<GridView
x:Name="itemGridView"
ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
</GridView>
In the code behind, I changed the code to the following:
DataModel.ComicDataWNSource source = new DataModel.ComicDataWNSource(App.Manager);
await source.ObtainRecentContent();
itemsViewSource.Source = source.Groups;
itemsViewSource.IsSourceGrouped = true;
this.itemsViewSource.ItemsPath = new PropertyPath("Items");
By making these changes, the code works. I'm now sure the thing I was doing wrong is that ItemsSource expects a Binding object which points to the CollectionViewSource object, not the actual CollectionViewSource object.
来源:https://stackoverflow.com/questions/20775294/windows-runtime-gridview-throws-argumentexception-when-setting-itemssource-prope