CollectionViewSource"值不在预期范围内."

Luk*_*ett 5 xaml winrt-xaml windows-store-apps

为什么此代码会在Windows 8 XAML应用程序中产生错误?

价值不在预期范围内.

XAML:

    <SemanticZoom>
        <SemanticZoom.ZoomedInView>
            <ListView
                Style="{StaticResource HorizontalListViewStyle}"
                SelectionMode="None"
                ScrollViewer.IsHorizontalScrollChainingEnabled="False"
                ItemsSource="{Binding BoardItems}" 
                ItemContainerStyle="{StaticResource ZoomedOutListViewItemContainerStyle}"
...
Run Code Online (Sandbox Code Playgroud)

MVVM代码:

ObservableCollection<WritingBoardModel> boards = new ObservableCollection<WritingBoardModel>();

... // Add item models to boards.

CollectionViewSource v = new CollectionViewSource()
{
    Source = boards,
};

this.ViewModel.Add(BoardItemsViewModelKey, v);
Run Code Online (Sandbox Code Playgroud)

如果我跳过CollectionViewSource并直接将boards实例添加到我的视图模型中,那么一切正常.

我想我需要使用a CollectionViewSource才能使一些语义缩放选择行为起作用.

卢克

Nat*_*ond 5

所以,CollectionViewSources很奇怪,你必须绑定它们的方式也很奇怪.举个例子,为了"按书"(样本项目的方式),我发现它实际上必须是StaticResource这样的:

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

<GridView ItemsSource="{Binding Source={StaticResource WhatevSource}}"/>
Run Code Online (Sandbox Code Playgroud)

请注意,我们没有直接将源设置为CollectionViewSource,但我们设置的是"无路径" Binding,基本上使用CollectionViewSourceas DataContext(只是一种方式来考虑它,实际上技术上并不正确).

这是我能够让它工作的唯一方法,不过我相信你在技术上可以在代码隐藏中ItemsSource直接设置视图CollectionViewSource或类似的东西.

希望这有助于编码!


Luk*_*ett 0

我需要像这样绑定到ViewCollectionViewSource 的属性:

CollectionViewSource v = new CollectionViewSource()
        {
            IsSourceGrouped = false,
            Source = boards,
        };

this.ViewModel.Add(BoardItemsViewModelKey, v.View);
Run Code Online (Sandbox Code Playgroud)

请注意,这对我的两个ListViews 和保持它们在SemanticZoom.