我有很长的项目列表,我需要在ListView中显示它们。然后我需要使用计时器自动旋转页面。怎么办?
答案 0 :(得分:0)
(Windows 8风格的UI / Metro应用)
在您的视图中:将{Binding PagedItems}添加到ListView的ItemSource属性中。
在ViewModel中:
public class ViewModel : INotifyPropertyChanged {
public ObservableCollection<Item> PagedItems {get;set;}
private DispatcherTimer _timer;
private ObservableCollection<Item> _itemsToPage;
private int _itemsPerPage;
private int _currentPage;
public ViewModel() {
_itemsToPage = new ObservableCollection<Item>(); // replace this with whatever your items are
_timer = new DispatcherTimer {Interval = new TimeSpan(0,0,0,10)};
_timer.Tick += NextPage;
_timer.Start();
}
private void NextPage(object sender, object e) {
if (_itemsToPage < _itemsPerPage) return;
if (_currentPage * _itemsPerPage >= _itemsToPage) _currentPage = 1;
else _currentPage++;
// the key line
PagedItems = new ObservableCollection<Item>(
_itemsToPage.Skip((_currentPage-1) * _itemsPerPage)
.Take(_itemsPerPage));
OnPropertyChanged("PagedItems") // you need to implement INotifyPropertyChanged
}
}
基本思路:每x秒根据您要分页的列表中的项目子集创建新列表。您会注意到(_currentPage-1) * _itemsPerPage
是每页上第一个项目的元素编号。