开发者

How can I get items from current page in PagedCollectionView?

I've got my objects in PagedCollectionView bound to DataGrid and DataPager.

var pcView = new PagedCollectionView(ObservableCollection<Message>(messages));开发者_开发技巧

How can I easily get items from current page in PagedCollectionView from my ViewModel? I wish there were something like this:

var messagesFromCurrentPage = pcView.CurrentPageItems; // error: no such a property

There are properties like SourceCollection, PageIndex and Count but I don't find them useful in this case. What am I missing here?


If you want to get select items you can just use Linq to do it.

var items = pcView.Where(i => i.SomeCondition == true);

Make sure you add a using statement for System.Linq.

Edit: Whenever I have a question as to what is really going on I just look at the code using Reflector (or ILSpy). In this case here is the relevant code inside GetEnumerator() which is how the Select or Where gets the items in the list:

    List<object> list = new List<object>();
    if (this.PageIndex < 0)
    {
        return list.GetEnumerator();
    }
    for (int i = this._pageSize * this.PageIndex; i < Math.Min(this._pageSize * (this.PageIndex + 1), this.InternalList.Count); i++)
    {
        list.Add(this.InternalList[i]);
    }
    return new NewItemAwareEnumerator(this, list.GetEnumerator(), this.CurrentAddItem);

So you can see how it is returning only the items in the current page from this code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜