這兩天需要給Datagrid加個分頁,查找了一些相關的文章,發現有一個寫了一個控件比較好,地址是 http://blog.csdn.net/zdw_wym/article/details/8221894
感謝這位大神12年的帖子,但是照着做了以后,發現除了點擊數字和GO按鈕好使意外,神馬“首頁、上一頁、下一頁、末頁”都不好使。
繼續找尋相關的資料和查看大神的源碼,發現有的地方寫的不對,因為textblock沒有click事件,而大神寫了click事件,所以沒有得到觸發,介於這個問題,我稍作了修改。
即給textblock添加里MouseLeftButtonUp事件,操作得以實現。
下面貼出全部源碼,有需要的,可以使用
翻頁控件xml文件

<UserControl x:Class="ImgProWPF.Paging" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"> <UserControl.Resources> <Style x:Key="PageTextBlock1" TargetType="{x:Type TextBlock}"> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="FontSize" Value="13"/> <Setter Property="FontWeight" Value="Bold"/> <Setter Property="Foreground" Value="#FF333333"/> </Style> <!--首頁上一頁等--> <Style x:Key="PageTextBlock2" TargetType="{x:Type TextBlock}"> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="Margin" Value="0,10,0,0"/> <Setter Property="Width" Value="40"/> <Setter Property="Height" Value="23"/> <Setter Property="FontSize" Value="13"/> <Setter Property="Cursor" Value="Hand"/> <Setter Property="Foreground" Value="#FF333333"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground" Value="#FF000000"/> <Setter Property="FontWeight" Value="Bold"/> </Trigger> </Style.Triggers> </Style> <!--中間頁數--> <Style x:Key="PageTextBlock3" TargetType="{x:Type TextBlock}"> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="Margin" Value="0,10,0,0"/> <Setter Property="Height" Value="23"/> <Setter Property="Width" Value="30"/> <Setter Property="FontSize" Value="10"/> <Setter Property="Cursor" Value="Hand"/> <Setter Property="Foreground" Value="#FF333333"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground" Value="#FF000000"/> <Setter Property="FontWeight" Value="Bold"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="#FF000000"/> <Setter Property="FontWeight" Value="Bold"/> </Trigger> </Style.Triggers> </Style> <Style x:Key="PageTextBox" TargetType="{x:Type TextBox}"> <Setter Property="Height" Value="25"/> <Setter Property="Width" Value="40"/> <Setter Property="BorderBrush" Value="{x:Null}"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="VerticalAlignment" Value="Bottom"/> <Style.Triggers> <Trigger Property="IsReadOnly" Value="True"> <Setter Property="Background" Value="#FFCCCCCC"/> </Trigger> </Style.Triggers> </Style> <Style x:Key="PageButton" TargetType="{x:Type Button}"> <Setter Property="Height" Value="25"/> <Setter Property="Width" Value="30"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="VerticalAlignment" Value="Bottom"/> </Style> </UserControl.Resources> <Grid> <Border CornerRadius="3" Background="Transparent" BorderBrush="{x:Null}"> <Grid HorizontalAlignment="Stretch" Margin="5 0 5 0" VerticalAlignment="Top" Width="Auto" Height="30"> <Grid.ColumnDefinitions> <ColumnDefinition Width="150"/> <ColumnDefinition Width="500" MinWidth="500"/> </Grid.ColumnDefinitions> <TextBlock Name="tbkRecords" Grid.Column="0" Style="{StaticResource PageTextBlock1}"/> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"/> <ColumnDefinition Width="50"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="50"/> <ColumnDefinition Width="50"/> <ColumnDefinition Width="50"/> <ColumnDefinition Width="30"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Name="btnFirst" Text="首頁" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseLeftButtonDown="btnFirst_MouseLeftButtonDown" MouseLeftButtonUp="btnFirst_MouseLeftButtonUp"/> <TextBlock Grid.Column="1" Name="btnPrev" Text="上一頁" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseLeftButtonDown="btnPrev_MouseLeftButtonDown" MouseLeftButtonUp="btnPrev_MouseLeftButtonUp"/> <Grid Grid.Column="2" Name="grid"> <Grid.RowDefinitions> <RowDefinition Height="30"/> </Grid.RowDefinitions> </Grid> <TextBlock Grid.Column="3" x:Name="btnNext" Text="下一頁" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseLeftButtonDown="btnNext_MouseLeftButtonDown" MouseLeftButtonUp="btnNext_MouseLeftButtonUp"/> <TextBlock Grid.Column="4" x:Name="btnLast" Text="末頁" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseLeftButtonDown="btnLast_MouseLeftButtonDown" MouseLeftButtonUp="btnLast_MouseLeftButtonUp"/> <TextBox Grid.Column="5" x:Name="pageGo" MaxLength="6" IsReadOnly="True" Style="{StaticResource PageTextBox}"/> <Button Grid.Column="6" x:Name="btnGo" Content="GO" IsEnabled="False" Style="{StaticResource PageButton}" Click="btnGo_Click"/> </Grid> </StackPanel> </Grid> </Border> </Grid> </UserControl>
翻頁控件.cs文件

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Data; using System.Text.RegularExpressions; namespace ImgProWPF { /// <summary> /// Paging.xaml 的交互邏輯 /// </summary> public partial class Paging : UserControl { public Paging() { InitializeComponent(); } #region 參數 private DataTable _dt = new DataTable(); //每頁顯示多少條 private int pageNum = 10; //當前是第幾頁 private int pIndex = 1; //對象 private DataGrid grdList; //最大頁數 private int MaxIndex = 1; //一共多少條 private int allNum = 0; #endregion #region 初始化數據 public void ShowPages(DataGrid grd, DataTable ds, int Num) { if (ds == null || ds.Rows.Count == 0) return; if (ds.Rows.Count == 0) return; DataTable dt = ds; this._dt = dt.Clone(); this.grdList = grd; this.pageNum = Num; this.pIndex = 1; foreach (DataRow r in dt.Rows) this._dt.ImportRow(r); SetMaxIndex(); ReadDataTable(); if(this.MaxIndex>1) { this.pageGo.IsReadOnly = false; this.btnGo.IsEnabled = true; } } #endregion #region 畫數據 private void ReadDataTable() { try { DataTable tmpTable = new DataTable(); tmpTable = this._dt.Clone(); int first = this.pageNum * (this.pIndex - 1); first = (first > 0) ? first : 0; //如果總數量大於每頁顯示數量 if (this._dt.Rows.Count >= this.pageNum * this.pIndex) { for (int i = first; i < pageNum * this.pIndex; i++) { tmpTable.ImportRow(this._dt.Rows[i]); } } else { for (int i = first; i < this._dt.Rows.Count; i++) { tmpTable.ImportRow(this._dt.Rows[i]); } } this.grdList.ItemsSource = tmpTable.DefaultView; tmpTable.Dispose(); } catch { MessageBox.Show("錯誤"); } finally { DisplayPagingInfo(); } } #endregion #region 畫每頁顯示的數據 private void DisplayPagingInfo() { if (this.pIndex == 1) { this.btnPrev.IsEnabled = false; this.btnFirst.IsEnabled = false; } else { this.btnPrev.IsEnabled = true; this.btnFirst.IsEnabled = true; } if (this.pIndex == this.MaxIndex) { this.btnNext.IsEnabled = false; this.btnLast.IsEnabled = false; } else { this.btnNext.IsEnabled = true; this.btnLast.IsEnabled = true; } this.tbkRecords.Text = string.Format("每頁{0}條/共{1}條", this.pageNum, this.allNum); int first = (this.pIndex - 4) > 0 ? (this.pIndex - 4) : 1; int last = (first + 9) > this.MaxIndex ? this.MaxIndex : (first + 9); this.grid.Children.Clear(); for (int i = first; i <= last; i++) { ColumnDefinition cdf = new ColumnDefinition(); this.grid.ColumnDefinitions.Add(cdf); TextBlock tbl = new TextBlock(); tbl.Text = i.ToString(); tbl.Style = FindResource("PageTextBlock3") as Style; tbl.MouseLeftButtonUp += new MouseButtonEventHandler(tbl_MouseLeftButtonUp); tbl.MouseLeftButtonDown += new MouseButtonEventHandler(tbl_MouseLeftButtonDown); if (i == this.pIndex) tbl.IsEnabled = false; Grid.SetColumn(tbl, this.grid.ColumnDefinitions.Count - 1); Grid.SetRow(tbl, 0); this.grid.Children.Add(tbl); } } #endregion #region 首頁 /// <summary> /// 首頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnFirst_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.pIndex = 1; ReadDataTable(); } private void btnFirst_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { e.Handled = true; } #endregion #region 上一頁 /// <summary> /// 上一頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrev_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (this.pIndex <= 1) return; this.pIndex--; ReadDataTable(); } private void btnPrev_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { e.Handled = true; } #endregion #region 下一頁 /// <summary> /// 下一頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNext_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (this.pIndex >= this.MaxIndex) return; this.pIndex++; ReadDataTable(); } private void btnNext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { e.Handled = true; } #endregion #region 末頁 /// <summary> /// 末頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLast_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.pIndex = this.MaxIndex; ReadDataTable(); } private void btnLast_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { e.Handled = true; } #endregion #region 設置最多頁面 /// <summary> /// 設置最多頁面 /// </summary> private void SetMaxIndex() { //多少頁 int Pages = this._dt.Rows.Count / pageNum; if (this._dt.Rows.Count != (Pages * pageNum)) { if (_dt.Rows.Count < (Pages * pageNum)) Pages--; else Pages++; } this.MaxIndex = Pages; this.allNum = this._dt.Rows.Count; } #endregion #region 跳轉到多少頁 /// <summary> /// 跳轉到多少頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGo_Click(object sender, RoutedEventArgs e) { if(IsNumber(this.pageGo.Text)) { int pageNum = int.Parse(this.pageGo.Text); if(pageNum>0&&pageNum<=this.MaxIndex) { this.pIndex = pageNum; ReadDataTable(); } else if(pageNum>this.MaxIndex) { this.pIndex = this.MaxIndex; ReadDataTable(); } } this.pageGo.Text = ""; } #endregion #region 分頁數字的點擊觸發事件 private void tbl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { TextBlock tbl = sender as TextBlock; if (tbl == null) return; int index = int.Parse(tbl.Text.ToString()); this.pIndex = index; if (index > this.MaxIndex) this.pIndex = this.MaxIndex; if (index < 1) this.pIndex = 1; ReadDataTable(); } private void tbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { e.Handled = true; } #endregion private static Regex RegNumber = new Regex("^[0-9]+$"); #region 判斷是否是數字 public static bool IsNumber(string valString) { Match m = RegNumber.Match(valString); return m.Success; } #endregion } }
主頁調用xml文件
<local:Paging x:Name="GridPaging"/>
主頁調用.cs文件
public void Band() { DBConfig DB = new DBConfig(); ds = DB.SearchAll(txtSearch.Text); DGInformation.ItemsSource = null; DGInformation.ItemsSource = ds.Tables[0].DefaultView;//Datagrid數據綁定 GridPaging.ShowPages(this.DGInformation, ds.Tables[0], 20);//分頁部分 }
效果圖展示
不知道是數據源里包含圖片的原因還是什么原因,在翻頁的時候,會很慢,等很長時間才能翻頁,這個問題,不知道是控件引起的,還是我綁定數據引起的,等解決了,我會在本帖說明。
還有一點就是,我啟用了WPF里的datagrid的序號,不過這個不像WEB里的分頁以后序號是連貫的,這個是重新分配,這個也是一個需要解決的問題。
實驗證明,翻頁緩慢是由使用datagrid自身的排序導致的,因此,采用SQL直接查詢序號,同原有數據一同綁定到datagrid,問題解決。