• WPF:ListView 分页-LMLPHP
  • 布局MainWindow.xaml
  •  <ListView Name="list_Reg"  ItemsSource="{Binding Source={StaticResource Data}}" Style="{StaticResource ListViewStyle}">
    <ListView.View>
    <GridView>
    <!--列头style-->
    <GridView.ColumnHeaderContainerStyle>
    <Style TargetType="GridViewColumnHeader">
    <Setter Property="Background" Value="CadetBlue"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="FontSize" Value="25"/>
    <Setter Property="Padding" Value="10,0"/>
    <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" Value="#FF1975A0"/>
    </Trigger>
    </Style.Triggers>
    </Style>
    </GridView.ColumnHeaderContainerStyle> <GridViewColumn Header="科室" DisplayMemberBinding="{Binding DeptName}"/>
    <GridViewColumn Header="开诊科目" DisplayMemberBinding="{Binding RegName}"/>
    <GridViewColumn Header="周一" DisplayMemberBinding="{Binding Show1}" Width="150"/>
    <GridViewColumn Header="周二" DisplayMemberBinding="{Binding Show2}" Width="150"/>
    <GridViewColumn Header="周三" DisplayMemberBinding="{Binding Show3}" Width="150"/>
    <GridViewColumn Header="周四" DisplayMemberBinding="{Binding Show4}" Width="150"/>
    <GridViewColumn Header="周五" DisplayMemberBinding="{Binding Show5}" Width="150"/>
    </GridView>
    </ListView.View>
    </ListView>
    </Canvas> <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal">
    <ItemsControl ItemsSource="{Binding Pages}">
    <ItemsControl.ItemTemplate>
    <DataTemplate>
    <Button Name="but_Pages" Content="{Binding Name}"
    Margin="5" Foreground="White" Background="CadetBlue" Padding="10,5" Click="but_Pages_Click"></Button>
    </DataTemplate>
    </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
    <WrapPanel/>
    </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    </ItemsControl> <TextBlock VerticalAlignment="Center">
    <TextBlock Text="【共"/>
    <TextBlock Text="{Binding Total}" Foreground="Yellow"/>
    <TextBlock Text="页】"/> <TextBlock Text="【当前"/>
    <TextBlock Text="{Binding CurrentSize}" Foreground="Yellow"/>
    <TextBlock Text="页】"/>
    </TextBlock>
    </StackPanel>
  • 每隔10秒翻页,循环
  •  public partial class MainWindow : Window
    {
    ViewPages viewPages = new ViewPages();
    Timer timer = new Timer();
    int _currCount=1;
    public MainWindow()
    {
    InitializeComponent();
    this.DataContext = viewPages;
    } private void window_Loaded(object sender, RoutedEventArgs e)
    {
    if (viewPages.Total > 1)
    {//当总页数大于1时才翻页
    timer.Interval = 10000;//5秒变换一次
    timer.Enabled = true;
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
    }
    } private void but_Pages_Click(object sender, RoutedEventArgs e)
    {//单击按钮时也翻页
    viewPages.Fun_Pager(((sender as Button).DataContext as Page).PageSize);
    } void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
    Dispatcher.BeginInvoke(new Action(() =>
    {
    //翻页
    _currCount = viewPages.CurrentSize+1;
    if (_currCount > viewPages.Total)
    {
    viewPages.CurrentSize = 1;
    _currCount = viewPages.CurrentSize;
    }));
    }
    }
  • 翻页函数 ViewPages.cs
  •     public class ViewPages:INotifyPropertyChanged
    {
    private int _number;
    public int Number
    {
    get { return _number; }
    set
    {
    _number = value;
    NotifyPropertyChanged("Number");
    }
    } private int _currentSize;
    public int CurrentSize
    {
    get { return _currentSize; }
    set
    {
    _currentSize = value;
    NotifyPropertyChanged("CurrentSize");
    }
    } private int _total;
    public int Total
    {
    get { return _total; }
    set
    {
    _total = value;
    NotifyPropertyChanged("Total");
    }
    } private List<Page> _pages;
    public List<Page> Pages
    {
    get { return _pages; }
    set
    {
    _pages = value;
    NotifyPropertyChanged("Pages");
    }
    } private List<WPF_OPRegDept.DataOP.Reg> _listRegDept;
    public List<WPF_OPRegDept.DataOP.Reg> ListRegDept
    {
    get { return _listRegDept; }
    set
    {
    _listRegDept = value;
    NotifyPropertyChanged("ListRegDept");
    }
    } private List<WPF_OPRegDept.DataOP.Reg> _listBind;
    public List<WPF_OPRegDept.DataOP.Reg> ListBind
    {
    get { return _listBind; }
    set
    {
    _listBind = value;
    NotifyPropertyChanged("ListBind");
    }
    } #region INotyfyPropertyChanged 成员
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string PropertyName)
    {
    if(PropertyChanged!=null)
    {
    PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
    }
    }
    #endregion public ViewPages()
    {
    DataOP dataOP=new DataOP();
    this.Number = 14;//设置每页显示数目
    this.ListRegDept = new List<DataOP.Reg>();//初始化数据
    ListRegDept = dataOP.GetRegDept(); this.Total = ListRegDept.Count() / this.Number + 1;//获取总页数 this.Pages = new List<Page>();//初始化所有页数数组
    for (int i = 1; i <= this.Total; i++)
    {
    this.Pages.Add(new Page {Name=i.ToString(),PageSize=i });
    }
    this.CurrentSize = 1;//设置当前显示页面
    Fun_Pager(this.CurrentSize); }
    /// <summary>
    /// 分页方法
    /// </summary>
    /// <param name="CurrentSize">当前页数</param>
    public void Fun_Pager(int CurrentSize)
    {
    this.CurrentSize = CurrentSize;
    this.ListBind = this.ListRegDept.Take(this.Number*this.CurrentSize)
    .Skip(this.Number*(this.CurrentSize-1)).ToList();
    }
    } public class Page
    {
    public string Name { get; set; }
    public int PageSize { get; set; }
    }
    }
05-11 19:38