Страницы

Поиск по вопросам

воскресенье, 26 января 2020 г.

WPF - Постраничная передача и отображение данных в приложение с DataGrid

#c_sharp #net #wpf #datagrid #silverlight


Имеется приложение, в котором для вывода данных используется DataGrid. Сейчас эти
данные загружаются в DataGrid из коллекции типа ObservableCollection, причём к DataGrid
добавил возможность постраничной навигации, для чего использовал PagedCollectionView
из Silverlight. Всё вроде как отлично работает. Теперь меня интересует, есть ли какие-то
встроенные средства добавить возможность не просто постраничного отображения, но и
также постраничной передачи данных в DataGrid (вернее даже не в DataGrid, а в CollectionView,
ассоциированному с этим DataGrid'ом)? Это было бы полезно в случае клиент-серверного
приложения, когда на стороне клиента будет лишь та порция данных, которая в данный
момент и отображается.

Спрашиваю про встроенные средства, т.к. в Silverlight постраничный DataGrid включён
в стандартную библиотеку. Учитывая, что Silverlight ориентирован на Веб, логично предположить,
что оный DataGrid как-то приспособлен для постепенной загрузки данных. Или это не так?
(С Silverlight не работал, так что его устройство не знаю.)


  Не уверен, что корректно сформулировал вопрос, так что если это так - поправьте
меня, пожалуйста.

    


Ответы

Ответ 1



Воспользуйтесь этим примером: using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using System.Windows.Data; namespace GridPagingExample { public partial class MainWindow : Window { private readonly PagingCollectionView _cview; public MainWindow() { InitializeComponent(); this._cview = new PagingCollectionView( new List { new { Animal = "Lion", Eats = "Tiger" }, new { Animal = "Tiger", Eats = "Bear" }, new { Animal = "Bear", Eats = "Oh my" }, new { Animal = "Wait", Eats = "Oh my isn't an animal" }, new { Animal = "Oh well", Eats = "Who is counting anyway" }, new { Animal = "Need better content", Eats = "For posting on stackoverflow" } }, 2 ); this.DataContext = this._cview; } private void OnNextClicked(object sender, RoutedEventArgs e) { this._cview.MoveToNextPage(); } private void OnPreviousClicked(object sender, RoutedEventArgs e) { this._cview.MoveToPreviousPage(); } } public class PagingCollectionView : CollectionView { private readonly IList _innerList; private readonly int _itemsPerPage; private int _currentPage = 1; public PagingCollectionView(IList innerList, int itemsPerPage) : base(innerList) { this._innerList = innerList; this._itemsPerPage = itemsPerPage; } public override int Count { get { if (this._currentPage < this.PageCount) // page 1..n-1 { return this._itemsPerPage; } else // page n { var itemsLeft = this._innerList.Count % this._itemsPerPage; if (0 == itemsLeft) { return this._itemsPerPage; // exactly itemsPerPage left } else { // return the remaining items return itemsLeft; } } } } public int CurrentPage { get { return this._currentPage; } set { this._currentPage = value; this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentPage")); } } public int ItemsPerPage { get { return this._itemsPerPage; } } public int PageCount { get { return (this._innerList.Count + this._itemsPerPage - 1) / this._itemsPerPage; } } private int EndIndex { get { var end = this._currentPage * this._itemsPerPage - 1; return (end > this._innerList.Count) ? this._innerList.Count : end; } } private int StartIndex { get { return (this._currentPage - 1) * this._itemsPerPage; } } public override object GetItemAt(int index) { var offset = index % (this._itemsPerPage); return this._innerList[this.StartIndex + offset]; } public void MoveToNextPage() { if (this._currentPage < this.PageCount) { this.CurrentPage += 1; } this.Refresh(); } public void MoveToPreviousPage() { if (this._currentPage > 1) { this.CurrentPage -= 1; } this.Refresh(); } } } Хамл: