Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C:System.ArgumentException(超出预期范围)_C#_Wpf_Xaml_Exception_Microsoft Metro - Fatal编程技术网

C# C:System.ArgumentException(超出预期范围)

C# C:System.ArgumentException(超出预期范围),c#,wpf,xaml,exception,microsoft-metro,C#,Wpf,Xaml,Exception,Microsoft Metro,当我尝试使用以下属性设置GridView的ItemsSource时,我得到了expection System.ArgumentExceptionValue不在预期范围内: 代码隐藏 OnNavigatedTo(NavigationEventArgs e) { ... itemGridView.ItemsSource = GetItems(NavigationParameter); // System.ArgumentException ... } GetItems方法

当我尝试使用以下属性设置GridView的ItemsSource时,我得到了expection System.ArgumentExceptionValue不在预期范围内:

代码隐藏

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。

使用现有CollectionViewSource并更新内容,而不是创建和返回新的CollectionViewSource:

OnNavigatedTo(NavigationEventArgs e) {
    ...
    GetItems(itemGridView.ItemsSource, NavigationParameter);
    ...
}

private void GetItems(CollectionViewSource source, 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() });

    source.Source = itemsByCategories.ToList();
    source.IsSourceGrouped = true;
    source.ItemsPath = new PropertyPath("Items");
}

可能的重复或关系?