哈尔滨网站备案手续,wordpress 移除字体,浙江省城乡建设信息港,thinkphp手机网站开发迟来的wpf分享。
目录
一、序言
二、前期准备 三、前端界面
四、后台代码部分
1、先定义些变量后面使用
2、先是按钮事件代码。
首页按钮
上一页按钮
下一页按钮
末尾按钮
画每页显示等数据
每页显示多少条
判断是否为数字的事件
分页数字的点击触发事件
跳转到…迟来的wpf分享。
目录
一、序言
二、前期准备 三、前端界面
四、后台代码部分
1、先定义些变量后面使用
2、先是按钮事件代码。
首页按钮
上一页按钮
下一页按钮
末尾按钮
画每页显示等数据
每页显示多少条
判断是否为数字的事件
分页数字的点击触发事件
跳转到多少页
3、泛型数据集合
设置页面多少
4、DataTable数据类型的
5、用户组件初始化
五、效果技术用方法
使用方法
效果
总结 一、序言
这里使用的工具是VS2019.net版本使用的是4.5版本。素材插件使用的是materialDesign。分享是随缘更新
二、前期准备
1、现在Nuget里安装MaterialDesignColorsMaterialDesignThemes两个插件包。
2、在App.xaml文件里更改成下面代码部分
Application.ResourcesResourceDictionaryResourceDictionary.MergedDictionariesResourceDictionary Sourcepack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml /!--ResourceDictionary Sourcepack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml /--ResourceDictionary Sourcepack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml /ResourceDictionary Sourcepack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml /ResourceDictionary Sourcepack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml /!--ResourceDictionary Sourcepack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml /--ResourceDictionary Sourcepack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Blue.xaml//ResourceDictionary.MergedDictionaries/ResourceDictionary/Application.Resources
3、在所需要使用的界面里引用materialDesign在这个位置添加如下代码 xmlns:materialDesignhttp://materialdesigninxaml.net/winfx/xaml/themes 三、前端界面 前期工作做好了开始写前端。这里写的是用户组件
前端代码如下
UserControl x:ClassComponentLibrary.WPF.UserControls.PagingTUserControlxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:localclr-namespace:ComponentLibrary.WPF.UserControlsmc:Ignorabled GridBorder CornerRadius3 BackgroundTransparent BorderBrush{x:Null}Grid HorizontalAlignmentStretch Margin5 0 5 0 VerticalAlignmentTop WidthAuto Height30Grid.ColumnDefinitionsColumnDefinition Width150/ColumnDefinition Width150/ColumnDefinition Widthauto MinWidth500//Grid.ColumnDefinitionsGridGrid.ColumnDefinitionsColumnDefinition/ColumnDefinition//Grid.ColumnDefinitionsLabel Content每页显示: HorizontalAlignmentRight/ComboBox x:NamecomInt PreviewMouseDowncomInt_PreviewMouseDown Grid.Column1//GridTextBlock NametbkRecords Grid.Column1 ForegroundWhite Style{StaticResource PageTextBlock1} /StackPanel OrientationHorizontal HorizontalAlignmentRight Grid.Column2GridGrid.RowDefinitions RowDefinition Height50/RowDefinition/Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition Width50/ColumnDefinition Width50/ColumnDefinition Width*/ColumnDefinition Width50/ColumnDefinition Width50/ColumnDefinition Width50/ColumnDefinition Width30//Grid.ColumnDefinitionsTextBlock Grid.Column0 NamebtnFirst Text首页 IsEnabledFalse Style{StaticResource FancyTextBlockStyle2} /TextBlock Grid.Column1 NamebtnPrev Text上一个 IsEnabledFalse Style{StaticResource FancyTextBlockStyle2} /Grid Grid.Column2 Namegrid Grid.RowDefinitionsRowDefinition Height30 /RowDefinition/Grid.RowDefinitions/GridTextBlock Grid.Column3 x:NamebtnNext Text下一个 IsEnabledFalse Style{StaticResource FancyTextBlockStyle2} /TextBlock Grid.Column4 x:NamebtnLast Text未页 IsEnabledFalse Style{StaticResource FancyTextBlockStyle2}/TextBox Grid.Column5 x:NamepageGo VisibilityCollapsed MaxLength6 IsReadOnlyTrue Style{StaticResource PageTextBox} /Button Grid.Column6 x:NamebtnGo VisibilityCollapsed ContentGO IsEnabledFalse Style{StaticResource PageButton} //Grid/StackPanel/Grid/Border/Grid
/UserControl这是它的一个原型。
四、后台代码部分 这里后台写了两种数据模式一个是泛型数据类型一个是DataTable数据类型。两种下面代码会逐一分享给大家。
1、先定义些变量后面使用
private DataTable _dt new DataTable();
/// summary/// 每页显示多少条/// /summaryprivate int pageNum 10;/// summary/// 当前是第几页/// /summaryprivate int pIndex 1;/// summary/// 对象/// /summaryprivate DataGrid grdList;/// summary/// 最大页数/// /summaryprivate int MaxIndex 1;/// summary/// 一共多少条/// /summaryprivate int allNum 0;/// summary/// 声明一个泛型对象接受数据./// /summaryprivate Listobject obj;
2、先是按钮事件代码。 首页按钮
#region 首页/// summary/// 首页/// /summary/// param namesender/param/// param namee/paramprivate void btnFirst_Click(object sender, System.EventArgs e){this.pIndex 1;ReadLsitT();}/// summary/// 首页/// /summary/// param namesender/param/// param namee/paramprivate void btnFirst_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){e.Handled true;}#endregion
上一页按钮 #region 上一页/// summary/// 上一页/// /summary/// param namesender/param/// param namee/paramprivate void btnPrev_Click(object sender, System.EventArgs e){if (this.pIndex 1)return;this.pIndex--;ReadLsitT();}/// summary/// 上一页/// /summary/// param namesender/param/// param namee/paramprivate void btnPrev_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){e.Handled true;}#endregion
下一页按钮 #region 下一页/// summary/// 下一页/// /summary/// param namesender/param/// param namee/paramprivate void btnNext_Click(object sender, System.EventArgs e){if (this.pIndex this.MaxIndex)return;this.pIndex;ReadLsitT();}/// summary/// 下一页/// /summary/// param namesender/param/// param namee/paramprivate void btnNext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){e.Handled true;}#endregion
末尾按钮 #region 未页/// summary/// 未页/// /summary/// param namesender/param/// param namee/paramprivate void btnLast_Click(object sender, System.EventArgs e){this.pIndex this.MaxIndex;ReadLsitT();}/// summary/// 未页/// /summary/// param namesender/param/// param namee/paramprivate void btnLast_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){e.Handled true;}#endregion
画每页显示等数据 #region 画每页显示等数据/// summary/// 画每页显示等数据/// /summaryprivate 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(FancyTextBlockStyle3) 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 每页显示多少条.private void comInt_PreviewMouseDown(object sender, MouseButtonEventArgs e){// 获取点击的 ComboBoxItemComboBoxItem clickedItem FindAncestorComboBoxItem((DependencyObject)e.OriginalSource);if (clickedItem ! null){// 获取点击项的内容显示的文本string clickedText clickedItem.Content.ToString();this.pageNum int.Parse(clickedText);SetMaxIndexList();ReadLsitT();}}/// summary/// 鼠标点击到combobox选项事件./// /summary/// typeparam nameT/typeparam/// param namecurrent/param/// returns/returnsprivate T FindAncestorT(DependencyObject current) where T : DependencyObject{do{if (current is T ancestor){return ancestor;}current VisualTreeHelper.GetParent(current);}while (current ! null);return null;}#endregion
判断是否为数字的事件 private static Regex RegNumber new Regex(^[0-9]$);#region 判断是否是数字/// summary/// 判断是否是数字/// /summary/// param namevalString/param/// returns/returnspublic static bool IsNumber(string valString){Match m RegNumber.Match(valString);return m.Success;}#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;ReadLsitT();}void tbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){e.Handled true;}#endregion
跳转到多少页 #region 跳转到多少页/// summary/// 跳转到多少页/// /summary/// param namesender/param/// param namee/paramprivate 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;ReadLsitT();}else if (pageNum this.MaxIndex){this.pIndex this.MaxIndex;ReadLsitT();}}this.pageGo.Text ;}#endregion
3、泛型数据集合 /// summary/// 定义一个泛型来接受数据/// /summary/// typeparam nameT/typeparam/// param nameparameter/param/// returns/returnspublic void GenericMethodT(ListT parameter){var data parameter;obj parameter.Castobject().ToList();}#region 返回泛型集合/// summary/// 初始化数据./// /summary/// typeparam nameT对象/typeparam/// param namedataGrid数据展示表格/param/// param nameparameter泛型/param/// param nameNum数量/parampublic void ShowPageTT(DataGrid dataGrid, ListT parameter, int Num){if (parameter null || parameter.Count 0)return;if (parameter.Count 0)return;this.grdList dataGrid;this.pageNum Num;this.pIndex 1;obj parameter.Castobject().ToList();SetMaxIndexList();ReadLsitT();if (this.MaxIndex 1){this.pageGo.IsReadOnly false;this.btnGo.IsEnabled true;}}/// summary/// 画数据/// /summarypublic void ReadLsitT(){try{int first this.pageNum * (this.pIndex - 1);first (first 0) ? first : 0;this.grdList.ItemsSource obj.Skip((this.pIndex - 1) * pageNum).Take(pageNum);}catch{MessageBox.Show(错误);}finally{DisplayPagingInfo();}}#endregion
上述的GenericMethod方法已经我这里没用使用你们可以参考使用扩展。
设置页面多少
#region 设置最多大页面/// summary/// 设置最多大页面/// /summarypublic void SetMaxIndexList(){//多少页int Pages this.obj.Count / pageNum;if (this.obj.Count ! (Pages * pageNum)){if (this.obj.Count (Pages * pageNum))Pages--;elsePages;}this.MaxIndex Pages;this.allNum this.obj.Count;}#endregion
4、DataTable数据类型的 #region 初始化数据/// summary/// 初始化数据datatable类型/// /summary/// param namegrd/param/// param namedtt/param/// param nameNum/parampublic void ShowPages(DataGrid grd, DataTable ds,int ss){if (ds null || ds.Rows.Count 0)return;if (ds.Rows.Count 0)return;ds.DefaultView.Sort Date DESC;DataTable dt ds.DefaultView.ToTable();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 画数据/// summary/// 画数据/// /summaryprivate 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();}}#endregion5、用户组件初始化 this.Loaded delegate{//首页this.btnFirst.MouseLeftButtonUp new MouseButtonEventHandler(btnFirst_Click);this.btnFirst.MouseLeftButtonDown new MouseButtonEventHandler(btnFirst_MouseLeftButtonDown);//上一页this.btnPrev.MouseLeftButtonUp new MouseButtonEventHandler(btnPrev_Click);this.btnPrev.MouseLeftButtonDown new MouseButtonEventHandler(btnPrev_MouseLeftButtonDown);//下一页this.btnNext.MouseLeftButtonUp new MouseButtonEventHandler(btnNext_Click);this.btnNext.MouseLeftButtonDown new MouseButtonEventHandler(btnNext_MouseLeftButtonDown);//末页this.btnLast.MouseLeftButtonUp new MouseButtonEventHandler(btnLast_Click);this.btnLast.MouseLeftButtonDown new MouseButtonEventHandler(btnLast_MouseLeftButtonDown);this.btnGo.Click new RoutedEventHandler(btnGo_Click);};
五、效果及使用方法
使用方法
这是我只展示泛型的使用方法
public void LogData(){try{LogDal logDal new LogDal();GlobalVariable.GlobalLog logDal.QueryByIdLogTable().OrderByDescending(m m.Date).ToList();this.Page.ShowPage1(this.logDataGrid, GlobalVariable.GlobalLog, 20); //这里是调用设置分页的函数}catch (Exception ex){throw ex;}}
只需要把你数据源和数据显示datagrid传递过去即可填写一个默认的每页显示的条数。 别忘了引用你的用户组卷目录。
效果 总结
到此分享结束源代码都在文章中简单整理即可使用。分享及随缘下次什么时候分享看缘分吧。