【发布时间】:2014-03-25 15:56:27
【问题描述】:
当我尝试使用以下属性设置 GridView 的 ItemsSource 时,我得到了预期 System.ArgumentException("Value does not fall within expected range"):
代码隐藏
OnNavigatedTo(NavigationEventArgs e) {
...
itemGridView.ItemsSource = GetItems(NavigationParameter); // System.ArgumentException
...
}
GetItems 方法
private CollectionViewSource GetItems(string key) {
var items = new List<Item>
{
new Item { Category = "blah", Title = "something" },
...
};
var itemsByCategories = unsortedItems.GroupBy(x => x.Category)
.Select(x => new ItemCategory { Title = x.Key, Items = x.ToList() });
var _foo = new CollectionViewSource();
_foo.Source = itemsByCategories.ToList();
_foo.IsSourceGrouped = true;
_foo.ItemsPath = new PropertyPath("Items");
return _foo;
}
为什么会出现此错误?
在 XAML 中,可以定义 CollectionViewSource 并将其设置为 ItemsSource。
【问题讨论】:
-
可能的重复或关系? stackoverflow.com/questions/20775294/…
标签: c# wpf xaml exception microsoft-metro