xaml
<Button FontSize="16" Margin="10,0" Style="{StaticResource ButtonPrimary}" Content="搜索" Click="SearchClick" Height="35"></Button> <StackPanel Grid.Row="2" HorizontalAlignment="Center"> <Grid Name="Pages" Visibility="Collapsed" Margin="5"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Name="tbCount" Text="共100条,每页10条,分10页" HorizontalAlignment="Right" Margin="0,0,10,0"></TextBlock> <Button Style="{StaticResource ButtonDefault}" Grid.Column="1" Margin="3,0" Name="btnFirst" FontSize="16" Content="首页" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="2" Margin="3,0" Name="btnPrev" Click="OnBtnClick" hc:IconElement.Geometry="{StaticResource LeftGeometry}"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="3" Margin="3,0" FontSize="16" Content="1" Name="btn1" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="4" Margin="3,0" FontSize="16" Content="2" Name="btn2" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="5" Margin="3,0" FontSize="16" Content="3" Name="btn3" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="6" Margin="3,0" FontSize="16" Content="4" Name="btn4" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="7" Margin="3,0" FontSize="16" Content="5" Name="btn5" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="8" Margin="3,0" FontSize="16" Content="6" Name="btn6" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="9" Margin="3,0" FontSize="16" Content="7" Name="btn7" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="10" Margin="3,0" FontSize="16" Content="8" Name="btn8" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="11" Margin="3,0" FontSize="16" Content="9" Name="btn9" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="12" Margin="3,0" FontSize="16" Content="10" Name="btn10" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="13" Margin="3,0" FontSize="16" Content="11" Name="btn11" Click="OnBtnClick"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="14" Margin="3,0" Name="btnNext" Click="OnBtnClick" hc:IconElement.Geometry="{StaticResource RightGeometry}"/> <Button Style="{StaticResource ButtonDefault}" Grid.Column="15" Name="btnLast" FontSize="16" Content="末页" Click="OnBtnClick"/> </Grid> </StackPanel>
cs代码
private void SearchClick(object sender, RoutedEventArgs e) { try { PageIndex = 1; GetPageData(); } catch (Exception ex) { Log4NetHelper.Err(ex); } } private int PageIndex = 1; private int PageSize = 15; private int PageCount; void GetPageData() { try { var AnswerStatus = cbAnswerStatus.SelectedIndex; var (list, allCount) = db.ConfereeCallRecordGetList(ChannelId, dpDt.SelectedDate.Value, AnswerStatus, tbNumber.Text, PageIndex, PageSize); dgRecordList.ItemsSource = list; //var allCount = ConfereeCallRecords.Count; //var noAnswerCount = ConfereeCallRecords.Count(it => it.DirectorAnswerTimes == 0 && it.AnchorAnswerTimes == 0); //var DirectorCount = ConfereeCallRecords.Count( it => it.DirectorAnswerTimes > 0); //var AnchorCount = ConfereeCallRecords.Count(it => it.AnchorAnswerTimes > 0); //var (list, allCount) = db.OfficialAccountGetList(loginWxid, SearchKeyWord, SearchType, PageIndex, PageSize); if (list == null) { //tbMsg.Text = "搜索出错了!"; //tbMsg.Foreground = new SolidColorBrush(Colors.Red); return; } if (PageIndex == 1) { PageCount = (int)Math.Ceiling(allCount * 1.0 / PageSize); tbCount.Text = $"共{allCount}条,每页{PageSize}条,分{PageCount}页"; btnFirst.IsEnabled=btnPrev.IsEnabled = false; } else { btnFirst.IsEnabled = btnPrev.IsEnabled = true; } btnLast.IsEnabled = btnNext.IsEnabled = true; if (PageIndex==PageCount) { btnLast.IsEnabled = btnNext.IsEnabled = false; } if (PageCount == 0) { Pages.Visibility = Visibility.Collapsed; } else { Pages.Visibility = Visibility.Visible; var beginIndex = Math.Max(1, PageIndex - 5); var endIndex = Math.Min(PageCount, PageIndex + 5); foreach (UIElement child in Pages.Children) { if (child is Button btn) { int.TryParse(btn.Name.Replace("btn", ""), out var j); if (j > 0) { var btnContent = beginIndex + j - 1; if (btnContent <= endIndex) { btn.Content = btnContent; } btn.IsEnabled = btnContent != PageIndex; btn.Visibility = btnContent <= endIndex ? Visibility.Visible : Visibility.Collapsed; } } } } } catch (Exception e) { Log4NetHelper.Err(e); } } /// <summary> /// 翻页按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnBtnClick(object sender, RoutedEventArgs e) { var btn = sender as Button; var btnContent = btn.Content?.ToString(); var ok = int.TryParse(btnContent, out var j); if (!ok) { var str = btn.Name.Replace("btn", ""); if (str == "First") { PageIndex = 1; } else if (str == "Prev") { PageIndex = Math.Max(1, PageIndex - 1); } else if (str == "Next") { PageIndex = Math.Min(PageCount, PageIndex + 1); } else//Last { PageIndex = PageCount; } } else { PageIndex = j; } GetPageData(); }View Code
搜索代码
public (List<ConfereeCallRecord>,int) ConfereeCallRecordGetList(int channelId, DateTime dt, int answerStatus, string numberKey, int pageIndex, int pageSize) { try { var allCount = 0; var db = GetDBInstance(0); var all = db.Queryable<ConfereeCallRecord>().Includes(it => it.Conferee).Where(it => channelId == it.ChannelId && it.CreatedAt >= dt && it.CreatedAt < dt.AddDays(1)); if (pageIndex == 1) { allCount = all.Count(); } var list = all.OrderByDescending(it => it.CreatedAt).Skip(pageIndex * pageSize - pageSize).Take(pageSize).ToList(); return (list, allCount); } catch (Exception e) { Log4NetHelper.Err(e); return (null,0); } }View Code
标签:PageIndex,翻页,PageCount,int,IsEnabled,allCount,var,wpf From: https://www.cnblogs.com/simadi/p/17754507.html