首页 > 其他分享 >wpf翻页

wpf翻页

时间:2023-10-10 14:16:01浏览次数:191  
标签:PageIndex 翻页 PageCount int IsEnabled allCount var wpf

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

相关文章

  • WPF之自己退出
    二、WPF退出窗口方法 1、强制退出,即使有其他的线程没有结束Environment.Exit(0);2、关闭当前程序,如果有其他线程没有结束,不会关闭System.Windows.Application.Current.Shutdown();3、强制终止当前进程,不仅关闭应用程序,还会关闭所有子进程System.Diagnostics.Process.GetCurr......
  • 界面控件DevExpress WPF地图组件,助力轻松搭建自己的地图数据服务器!
    DevExpressWPF Map控件允许用户在WPF应用程序中合并所有流行的地图服务,如Bing或OpenStreetMap。您还可以在公司网络中创建自己的地图数据服务器,Map组件完全支持矢量和笛卡尔坐标地图,以及Bing地理代码、搜索、路线和海拔服务。P.S:DevExpressWPF拥有120+个控件和库,将帮助您交付......
  • 【WPF】单例软件实现自重启
    原文地址https://www.cnblogs.com/younShieh/p/17749694.html❤如果本文对你有所帮助,不妨点个关注和推荐呀,这是对笔者最大的支持~❤ 在WPF应用程序中,想要实现软件重启,可以再Start一次该软件的exe程序。但是有些时候我们想要这个程序是唯一运行的单例,而不是可以多开的程序。......
  • 【项目二】WPF掷骰子
    一、素材地址:https://icons8.com/icons/set/dice二、需求分析:WPF框架实现一个掷骰子动画:有6个点数的骰子图片,初始时图片默认为1点,当点击开始按钮后,随机变换图片,2s后定格到当前骰子点数。三、代码实现:1.需要将骰子的6张图片放在项目的"Images"文件夹下,并设置它们的BuildActio......
  • WPF实现语言切换
    本示例实现的是中英文语言切换。开发环境:VS2022、WIN10 一、新建中英文两个key-value对照文件。 二、添加到App.xaml文件。 三、在App.xaml代码文件中创建切换语言和根据key获取value值的方法。usingSystem.Collections;usingSystem.Configuration;usingSystem......
  • WPF 上位机软件 开发小技巧
    本文长期更新... 1.鼠标按下拖拽窗体 自定义窗体的界面,右键按下鼠标后,可移动窗体的代码注册 MouseLeftButtonDown事件///<summary>///鼠标左键点击可移动窗体///</summary>///<paramname="sender"></param>///<par......
  • WPF
    https://baike.baidu.com/item/WPF/5299594?fr=aladdinWPF(WindowsPresentationFoundation)是微软推出的基于Windows的用户界面框架,属于.NETFramework3.0的一部分。它提供了统一的编程模型、语言和框架,真正做到了分离界面设计人员与开发人员的工作;同时它提供了全新的多媒体交......
  • WPF ABP框架更新(2023-10月份)
    更新说明本次更新主要内容以下:优化UI显示样式,按钮、文字显示模糊、边距一致性更新Syncfusion版本框架版本升级至.NET7ABP版本升级至8.0......
  • Wpf经验技巧-使用 d:DataContext 指定 DataContext 的类型.
    VM代码:V代码(版本1):没有指定DataContext的类型,所以下面的绑定并不知道P1和P3到底是什么,也就无法在代码编辑时检测出绑定是否正确.如果写错了,只能等到程序运行并打开这个窗口时报错才能知道.V代码(版本2):通过d:DataContext指定了DataContext的类型,所以下面的绑定......
  • WPF开发记录
    字符串格式化XXX.tostring():5.tostring("D2")结果为05Datetime.Now.ToString("yyyy-MM-dd")结果为2023-10-03在xaml中使用String.Format转换:<TextboxBinding="{BindingDateTimeNow,String.Format{}{0:yyyy-MM-dd}}"IsReadOnly="True"......