using DevExpress.XtraEditors; using DevExpress.XtraPrinting.Native; using DW5S.DTO; using DW5S.Entity; using DW5S.Repostory; using ExtensionsDev; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using System.Windows.Forms; namespace DW5S.App.UserControl { public partial class CtrlPage : DevExpress.XtraEditors.XtraUserControl { private event Func> OnQueryAsync; public void SetOnQuery(Func> onQueryAsync) { this.OnQueryAsync += onQueryAsync; RefreshData(); } private bool comPact = false; /// /// 紧凑模式(默认false) /// [ToolboxItem(false)] [DefaultValue(false)] [Category("Style"), Description("紧凑模式")] public bool Compact { get { return comPact; } set { comPact = value; if (comPact) { foreach (Control item in this.stackPanel1.Controls) { item.Margin = new Padding(2); if (item.GetType() == typeof(Button)) item.Width = 14; } txtCountPerPage.Width = 60; txtGoPage.Width = 60; } else { foreach (Control item in this.stackPanel1.Controls) { item.Margin = new Padding(2); if (item.GetType() == typeof(Button)) item.Width = 20; } btnFirst.Margin = new System.Windows.Forms.Padding(20, 3, 3, 3); txtPageInfo.Margin = new System.Windows.Forms.Padding(10, 3, 10, 3); txtGoPage.Margin = new System.Windows.Forms.Padding(20, 3, 3, 3); this.txtCountPerPage.Width = 76; txtGoPage.Width = 76; } } } public bool showGoto = true; /// /// 是否显示跳转(模式true) /// [ToolboxItem(true)] [Category("Style"), Description("是否显示跳转")] [DefaultValue(true)] public bool ShowGoto { get { return showGoto; } set { showGoto = value; this.txtGoPage.Visible = value; this.btnGo.Visible = value; } } private bool alignRight = false; /// /// 是否右对齐(默认false) /// [ToolboxItem(false)] [DefaultValue(false)] [Category("Style"), Description("右对齐")] public bool AlignRight { get { return alignRight; } set { alignRight = value; if (alignRight) { labelControl1.Text = " "; stackPanel1.SetStretched(labelControl1, true); } else { labelControl1.Text = ""; stackPanel1.SetStretched(labelControl1, false); } } } /// /// 当前页数 /// private int CurrentPage { get; set; } = 1; public CtrlPage() { InitializeComponent(); this.MinimumSize = new System.Drawing.Size(100, 26); } /// /// 刷新数据(当前页会变为1) /// public void RefreshData() { btn_Click(btnFirst, null); } private void txtCountPerPage_EditValueChanged(object sender, EventArgs e) { btn_Click(btnFirst, null); } private async void btn_Click(object sender, EventArgs e) { try { if (OnQueryAsync == null) return; if (sender == btnFirst) { this.CurrentPage = 1; } else if (sender == btnPrev) { if (this.CurrentPage > 1) this.CurrentPage--; } else if (sender == btnNext) { this.CurrentPage++; } else if (sender == btnLast) { this.CurrentPage = int.MaxValue; } else { int.TryParse(txtGoPage.Text, out int currentPage); if (currentPage <= 0) return; this.CurrentPage = currentPage; } int.TryParse(txtCountPerPage.Text, out int countPerPage); if (countPerPage <= 0) { countPerPage = (int)txtCountPerPage.Properties.Items[0]; } this.Enabled = false; var pageData = await this.OnQueryAsync(new PageQueryDto() { CountPerPage = countPerPage, Page = CurrentPage, }); if (pageData == null) { pageData = new DTO.PageData() { CountPerPage = 100, Page = 1, TotalCount = 0, }; } this.CurrentPage = pageData.Page; txtPageInfo.Text = $"第{pageData.Page}页/共{pageData.TotalPage}页"; } catch (Exception ex) { IocContainer.Logger.Error(ex, $"分页查询异常"); } finally { this.Enabled = true; } } } }