NavigationPager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. namespace FWindSoft.Wpf.Controls
  10. {
  11. /// <summary>
  12. /// 导航分页
  13. /// </summary>
  14. public class NavigationPager : Control, INotifyPropertyChanged
  15. {
  16. public static readonly DependencyProperty AttachElementProperty = DependencyProperty.Register("AttachElement",
  17. typeof(UIElement),
  18. typeof(NavigationPager), new PropertyMetadata(null));
  19. public static readonly RoutedEvent PageIndexChangingEvent = EventManager.RegisterRoutedEvent("PageIndexChanging",
  20. RoutingStrategy.Direct, typeof(EventHandler<PageChangingEventArgs>), typeof(NavigationPager));
  21. public static readonly RoutedEvent PageIndexChangedEvent = EventManager.RegisterRoutedEvent("PageIndexChanged",
  22. RoutingStrategy.Direct, typeof(EventHandler<PageChangedEventArgs>), typeof(NavigationPager));
  23. public static readonly DependencyProperty CurrentPageIndexProperty;
  24. public static readonly DependencyProperty TotalPageCountProperty;
  25. public static readonly DependencyProperty TotalPageVisibleProperty;
  26. static NavigationPager()
  27. {
  28. DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigationPager), new FrameworkPropertyMetadata(typeof(NavigationPager)));
  29. CurrentPageIndexProperty = DependencyProperty.Register("CurrentPageIndex", typeof(int), typeof(NavigationPager), new PropertyMetadata(1, new PropertyChangedCallback(OnCurrentPageChanged)));
  30. TotalPageCountProperty = DependencyProperty.Register("TotalPageCount", typeof(int), typeof(NavigationPager), new PropertyMetadata(0, new PropertyChangedCallback(OnPageCountChanged)));
  31. TotalPageVisibleProperty = DependencyProperty.Register("TotalPageVisible", typeof(bool), typeof(NavigationPager), new PropertyMetadata(true, null));
  32. }
  33. /// <summary>
  34. /// 当前页索引
  35. /// </summary>
  36. public int CurrentPageIndex
  37. {
  38. get { return (int)GetValue(CurrentPageIndexProperty); }
  39. set { SetValue(CurrentPageIndexProperty, value); }
  40. }
  41. /// <summary>
  42. /// 总页数
  43. /// </summary>
  44. public int TotalPageCount
  45. {
  46. get { return (int)GetValue(TotalPageCountProperty); }
  47. set { SetValue(TotalPageCountProperty, value); }
  48. }
  49. /// <summary>
  50. /// 总页数是否显示
  51. /// </summary>
  52. public bool TotalPageVisible
  53. {
  54. get { return (bool)GetValue(TotalPageVisibleProperty); }
  55. set { SetValue(TotalPageVisibleProperty, value); }
  56. }
  57. /// <summary>
  58. /// 附加元素
  59. /// </summary>
  60. public UIElement AttachElement
  61. {
  62. get { return this.GetValue(AttachElementProperty) as UIElement; }
  63. set { this.SetValue(AttachElementProperty, value); }
  64. }
  65. private static void OnCurrentPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  66. {
  67. NavigationPager p = d as NavigationPager;
  68. p?.ChangePageIndex((int) e.OldValue, (int) e.NewValue);
  69. //p?.NotifyBtnEnabled();
  70. }
  71. private static void OnPageCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  72. {
  73. NavigationPager p = d as NavigationPager;
  74. p?.NotifyBtnEnabled();
  75. }
  76. #region 模板元素操作
  77. public override void OnApplyTemplate()
  78. {
  79. base.OnApplyTemplate();
  80. var btnFirst = this.Template.FindName("BtnFirst", this) as Button;
  81. if (btnFirst != null)
  82. {
  83. btnFirst.Click += BtnFirst_Click;
  84. }
  85. var btnPrevious = this.Template.FindName("BtnPrevious", this) as Button;
  86. if (btnPrevious != null)
  87. {
  88. btnPrevious.Click += BtnPrevious_Click; ;
  89. }
  90. var btnNext = this.Template.FindName("BtnNext", this) as Button;
  91. if (btnNext != null)
  92. {
  93. btnNext.Click += BtnNext_Click;
  94. }
  95. var btnLast = this.Template.FindName("BtnLast", this) as Button;
  96. if (btnLast != null)
  97. {
  98. btnLast.Click += BtnLast_Click;
  99. }
  100. }
  101. private void BtnLast_Click(object sender, RoutedEventArgs e)
  102. {
  103. SetCurrentPageIndex(this.TotalPageCount);
  104. }
  105. private void BtnNext_Click(object sender, RoutedEventArgs e)
  106. {
  107. SetCurrentPageIndex(Math.Min(this.CurrentPageIndex+1, this.TotalPageCount)) ;
  108. }
  109. private void BtnPrevious_Click(object sender, RoutedEventArgs e)
  110. {
  111. SetCurrentPageIndex(Math.Max(this.CurrentPageIndex - 1, 1));
  112. }
  113. private void BtnFirst_Click(object sender, RoutedEventArgs e)
  114. {
  115. SetCurrentPageIndex(1);
  116. }
  117. #endregion
  118. /// <summary>
  119. /// 能跳转到第一个
  120. /// </summary>
  121. public bool CanGoFirst
  122. {
  123. get { return this.CurrentPageIndex > 1; }
  124. }
  125. /// <summary>
  126. /// 能跳转到最后一个
  127. /// </summary>
  128. public bool CanGoLast
  129. {
  130. get { return this.CurrentPageIndex < TotalPageCount; }
  131. }
  132. #region 相关事件处理
  133. /// <summary>
  134. /// 是否是内部编辑
  135. /// </summary>
  136. private bool InnerEditing { get; set; }
  137. public void BeginInEdit()
  138. {
  139. InnerEditing = true;
  140. }
  141. public void EndInEdit()
  142. {
  143. InnerEditing = false;
  144. }
  145. public void SetCurrentPageIndex(int index)
  146. {
  147. ChangePageIndex(this.CurrentPageIndex, index);
  148. }
  149. private void ChangePageIndex(int oldIndex, int newIndex)
  150. {
  151. if (InnerEditing)
  152. return;
  153. try
  154. {
  155. BeginInEdit();
  156. //index相同也要重新执行,当刷新
  157. var eventArgs = new PageChangingEventArgs() { OldPageIndex = oldIndex, NewPageIndex = newIndex };
  158. eventArgs.RoutedEvent = PageIndexChangingEvent;
  159. eventArgs.Source = this;
  160. this.RaiseEvent(eventArgs);
  161. if (!eventArgs.IsCancel)
  162. {
  163. this.CurrentPageIndex = newIndex;
  164. var eventChangedArgs = new PageChangedEventArgs() { CurrentPageIndex = newIndex};
  165. eventChangedArgs.RoutedEvent = PageIndexChangedEvent;
  166. eventChangedArgs.Source = this;
  167. this.RaiseEvent(eventChangedArgs);
  168. }
  169. else
  170. {
  171. this.CurrentPageIndex = oldIndex;
  172. }
  173. }
  174. finally
  175. {
  176. EndInEdit();
  177. NotifyBtnEnabled();
  178. }
  179. }
  180. public event EventHandler<PageChangingEventArgs> PageIndexChanging
  181. {
  182. add { this.AddHandler(PageIndexChangingEvent, value); }
  183. remove { this.RemoveHandler(PageIndexChangingEvent, value); }
  184. }
  185. public event EventHandler<PageChangedEventArgs> PageIndexChanged
  186. {
  187. add { this.AddHandler(PageIndexChangedEvent, value); }
  188. remove { this.RemoveHandler(PageIndexChangedEvent, value); }
  189. }
  190. #endregion
  191. #region INotifyPropertyChanged成员
  192. public event PropertyChangedEventHandler PropertyChanged;
  193. public void NotifyPropertyChanged(string propertyName)
  194. {
  195. if (this.PropertyChanged != null)
  196. {
  197. this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
  198. }
  199. }
  200. public void NotifyBtnEnabled()
  201. {
  202. NotifyPropertyChanged(nameof(CanGoFirst));
  203. NotifyPropertyChanged(nameof(CanGoLast));
  204. }
  205. #endregion
  206. }
  207. }