Windows Phone的CollectionViewSort没有显示任何东西 [英] Windows Phone CollectionViewSort not showing anything

查看:160
本文介绍了Windows Phone的CollectionViewSort没有显示任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想其中有在顶部的搜索框和匹配的搜索下方,更新的搜索框更新项目列表的WP8应用程序开发的页面。很简单,对不对?但我无法得到它的工作。

I am trying to develop a page in a WP8 app which has a search box at the top and a list of items that match the search below, updating as the search box updates. Pretty straightforward, right? Except I can't get it to work.

细读计算器和趣闻后,推荐的解决方案似乎是使用 CollectionViewSource 。好吧,我尝试使用它,并没有项目出现。当我切换到的ObservableCollection 包含我的XAML的项目,一切都显示了罚款。

After perusing StackOverflow and the interwebz, the recommended solution seems to be to use CollectionViewSource. Well, I'm trying to use it and no items show up. As soon as I switch to the ObservableCollection containing my items in XAML, everything shows up fine.

我的数据是从DB异步加载。

My data is loaded asynchronously from a DB.

XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <toolkit:PhoneTextBox Grid.Row="0" Hint="search query" ActionIcon="/Assets/Search.png" Text="{Binding SearchQuery, Mode=TwoWay}">
        <i:Interaction.Behaviors>
            <themes:TextBoxUpdateOnTextChangedBehavior />
        </i:Interaction.Behaviors>
    </toolkit:PhoneTextBox>
    <phone:LongListSelector Grid.Row="1" ItemsSource="{Binding SearchResults}" />
    <!-- I have also tried binding to SearchResults.View with no success -->
</Grid>



视图模型

public class MyViewModel
{
    private ObservableCollection<MyItemViewModel> _allItems = new ObservableCollection<MyItemViewModel>();

    public CollectionViewSource SearchResults { get; private set; }

    public MyViewModel()
    {
        SearchResults = new CollectionViewSource { Source = _allItems };
        _allItems.CollectionChanged += (_, __) => SearchResults.View.Refresh();

        LoadAllItemsAsync();
    }

    private async void LoadAllItemsAsync()
    {
        IList<MyItemModel> models = await LoadMyModels();
        _allItems.AddRange(models.Select(model => new MyItemViewModel(model)));
    }
}



正如你所看到的,我都没有尝试写尚未过滤代码。应该不高于基本代码显示与我已经加载的所有项目LongListSelector?如果我改变了的SearchResult 键入的ObservableCollection< MyItemViewModel> 并有吸气回 _allItems ,我的网页显示预期的项目。我在做什么错在这里?

As you can see, I haven't even attempted to write filtering code yet. Shouldn't the code above basically show a LongListSelector with all the items I have loaded? If I change the SearchResults type to ObservableCollection<MyItemViewModel> and have the getter return _allItems, my page shows the expected items. What am I doing wrong here?

推荐答案

的问题是, LongListSelector 控制要求其的ItemsSource 工具的IList ,其中CollectionViewSource不做。这意味着你不能绑定一个 CollectionViewSource LongListSelector 开箱。你可以尝试将其设置在代码隐藏文件中像这样看到这一点:

The problem is that the LongListSelector control requires that its ItemsSource implements IList, which CollectionViewSource doesn't do. This means you can't bind a CollectionViewSource to a LongListSelector out of the box. You can see this by trying to set it in the code-behind file like this:

myLongListSelector.ItemsSource = myCollectionViewSource.View;



这给出了一个错误说,它不能视图转换为的IList

我知道ATLEAST两个解决办法,一个是写周围CollectionViewSource一个包装类,并实现IList,然后绑定到这一点。另一个问题给出了一个这样的例子(我还没有尝试过这样想):的 Windows Phone中使用的CollectionView LongListSelector分组7或Windows Phone 8

I know atleast two solutions to this, one is to write a wrapper class around CollectionViewSource which does implement IList and then bind to that. Another question gives an example of this (I haven't tried it thought): LongListSelector grouping using CollectionView in Windows Phone 7 or Windows Phone 8

另一种方法是不使用 CollectionViewSource 键,而不是使用 System.Linq的进行排序就像这个问题建议:的如何排序的Windows Phone

Another method is to not use CollectionViewSource and instead using System.Linq for sorting like suggested in this question: How to Sort a LongListSelector in Windows Phone

这篇关于Windows Phone的CollectionViewSort没有显示任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆