ListBox and ListView in the application, there is often a need to display a fixed amount of data per page, and then through the timer automatic or manual paging operation, this article is about the implementation of this action.
I. Focus
For the listbox and ListView, the Itemsource bindings for the background bindings are generally in list<t> format, and List<t> has a method of take and skip, which means taking the list part and skipping the list section.
The format for fetching data is: List.take (). Skip ();
Two. Not much to say, the instance speaks (the example link will be attached later, for reference only)
(1) on the XAML interface:
<StackPanel> <listbox height=" -"scrollviewer.verticalscrollbarvisibility="Disabled"Itemssource="{Binding Listshow}"></ListBox> <button x:name="Turnupbutton"Content="Page up"click="Turnpageup"height=" -"/> <button x:name="Turndownbutton"Content="PAGE DOWN"click="Turnpagedown"height=" -"/> <button content="Automatic Paging"click="Button_click_1"height=" -"/> </StackPanel>
The listbox is the data display area, and the content of the three button has its own function.
(2) CS code
Public Partial classMainwindow:window { Publicobservablecollection<string>Listshow {Get{return(observablecollection<string>) GetValue (Listshowproperty); } Set{SetValue (listshowproperty, value);} } Publiclist<string> namelist =Newlist<string>(); PublicMainWindow () {InitializeComponent (); //WPF Data Binding This. DataContext = This; //Fake Data CreationData (); //Data Displaycontentshow (); //timer is initialized to auto-page preparationTimeset (); } Private voidData () { for(inti =0; I < -; i++) { strings ="Name"+i.tostring (); Namelist.add (s); } } Private voidcontentshow () {listshow=Newobservablecollection<string> (Namelist.take (PageSize * Page). Skip (PageSize * (Page-1))); if(namelist.count% PageSize = =0) { Total= Namelist.count/PageSize; } Else{ Total= Namelist.count/pagesize +1; } turnupbutton.visibility= Page >1?Visibility.Visible:Visibility.Hidden; Turndownbutton.visibility= Page < total?Visibility.Visible:Visibility.Hidden; } Private voidTimeset () {Timer=NewDispatcherTimer (); Timer. Interval= Timespan.fromseconds (5); Timer. Tick+=Timer_tick; } voidTimer_tick (Objectsender, EventArgs e) { if(Page <Total ) {Page++; Contentshow (); } Else{Page=1; Contentshow (); } } Private voidButton_click_1 (Objectsender, RoutedEventArgs e) { if(IsOpen = =true) {IsOpen=false; Timer. Stop (); } Else{timer. Start (); IsOpen=true; } } Private voidTurnpageup (Objectsender, RoutedEventArgs e) {Page--; Contentshow (); } Private voidTurnpagedown (Objectsender, RoutedEventArgs e) {Page++; Contentshow (); } DispatcherTimer Timer; Private BOOLIsOpen; Private intPage =1; Private intTotal ; Private intPageSize =7; Public Static ReadOnlyDependencyProperty Listshowproperty =Dependencyproperty.register ("Listshow",typeof(observablecollection<string>),typeof(MainWindow),NewPropertyMetadata (Newobservablecollection<string>())); }
Listshow is a itemsource bound object in the XAML listbox, and all data is displayed to the interface through Listshow.
Emphasize must use observablecollection<t> type, so in order to change the listshow data, the interface automatically refresh, the normal list<t> assignment will not refresh the interface.
page--the current number of pages. pagesize--number of data per page. total--Total pages.
New observablecollection<string1));
Take the pagesize*page data, skip skipping (pagesize* (Page-1)) data, the actual acquisition is 1 pages of data, that is, 1 PageSize.
The following two code is to limit the page on the first page and last page, can not continue to flip and rollover, and set the visibility is not visible.
1 ? Visibility.Visible:Visibility.Hidden; = Page < total? Visibility.Visible:Visibility.Hidden;
Three. Easy to wrong point
(1) The timer must use DispatcherTimer, if only with the timer will be because of threading problems can not achieve automatic paging.
(2) The last new Propertymedata in the dependency property register is new out an empty observablecollection<string>(), Not String.Empty or null
Four. Code download
WPF in the ListBox ListView data Page view Note (emphasis: Is data paging, non-paging animation)