123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- namespace FWindSoft.Wpf.Controls
- {
- /// <summary>
- /// 导航分页
- /// </summary>
- public class NavigationPager : Control, INotifyPropertyChanged
- {
- public static readonly DependencyProperty AttachElementProperty = DependencyProperty.Register("AttachElement",
- typeof(UIElement),
- typeof(NavigationPager), new PropertyMetadata(null));
- public static readonly RoutedEvent PageIndexChangingEvent = EventManager.RegisterRoutedEvent("PageIndexChanging",
- RoutingStrategy.Direct, typeof(EventHandler<PageChangingEventArgs>), typeof(NavigationPager));
- public static readonly RoutedEvent PageIndexChangedEvent = EventManager.RegisterRoutedEvent("PageIndexChanged",
- RoutingStrategy.Direct, typeof(EventHandler<PageChangedEventArgs>), typeof(NavigationPager));
- public static readonly DependencyProperty CurrentPageIndexProperty;
- public static readonly DependencyProperty TotalPageCountProperty;
- public static readonly DependencyProperty TotalPageVisibleProperty;
- static NavigationPager()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigationPager), new FrameworkPropertyMetadata(typeof(NavigationPager)));
- CurrentPageIndexProperty = DependencyProperty.Register("CurrentPageIndex", typeof(int), typeof(NavigationPager), new PropertyMetadata(1, new PropertyChangedCallback(OnCurrentPageChanged)));
- TotalPageCountProperty = DependencyProperty.Register("TotalPageCount", typeof(int), typeof(NavigationPager), new PropertyMetadata(0, new PropertyChangedCallback(OnPageCountChanged)));
- TotalPageVisibleProperty = DependencyProperty.Register("TotalPageVisible", typeof(bool), typeof(NavigationPager), new PropertyMetadata(true, null));
- }
- /// <summary>
- /// 当前页索引
- /// </summary>
- public int CurrentPageIndex
- {
- get { return (int)GetValue(CurrentPageIndexProperty); }
- set { SetValue(CurrentPageIndexProperty, value); }
- }
- /// <summary>
- /// 总页数
- /// </summary>
- public int TotalPageCount
- {
- get { return (int)GetValue(TotalPageCountProperty); }
- set { SetValue(TotalPageCountProperty, value); }
- }
- /// <summary>
- /// 总页数是否显示
- /// </summary>
- public bool TotalPageVisible
- {
- get { return (bool)GetValue(TotalPageVisibleProperty); }
- set { SetValue(TotalPageVisibleProperty, value); }
- }
- /// <summary>
- /// 附加元素
- /// </summary>
- public UIElement AttachElement
- {
- get { return this.GetValue(AttachElementProperty) as UIElement; }
- set { this.SetValue(AttachElementProperty, value); }
- }
- private static void OnCurrentPageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- NavigationPager p = d as NavigationPager;
- p?.ChangePageIndex((int) e.OldValue, (int) e.NewValue);
- //p?.NotifyBtnEnabled();
- }
- private static void OnPageCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- NavigationPager p = d as NavigationPager;
- p?.NotifyBtnEnabled();
- }
-
- #region 模板元素操作
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- var btnFirst = this.Template.FindName("BtnFirst", this) as Button;
- if (btnFirst != null)
- {
- btnFirst.Click += BtnFirst_Click;
- }
- var btnPrevious = this.Template.FindName("BtnPrevious", this) as Button;
- if (btnPrevious != null)
- {
- btnPrevious.Click += BtnPrevious_Click; ;
- }
- var btnNext = this.Template.FindName("BtnNext", this) as Button;
- if (btnNext != null)
- {
- btnNext.Click += BtnNext_Click;
- }
- var btnLast = this.Template.FindName("BtnLast", this) as Button;
- if (btnLast != null)
- {
- btnLast.Click += BtnLast_Click;
- }
- }
- private void BtnLast_Click(object sender, RoutedEventArgs e)
- {
- SetCurrentPageIndex(this.TotalPageCount);
- }
- private void BtnNext_Click(object sender, RoutedEventArgs e)
- {
- SetCurrentPageIndex(Math.Min(this.CurrentPageIndex+1, this.TotalPageCount)) ;
- }
- private void BtnPrevious_Click(object sender, RoutedEventArgs e)
- {
- SetCurrentPageIndex(Math.Max(this.CurrentPageIndex - 1, 1));
- }
- private void BtnFirst_Click(object sender, RoutedEventArgs e)
- {
- SetCurrentPageIndex(1);
- }
- #endregion
- /// <summary>
- /// 能跳转到第一个
- /// </summary>
- public bool CanGoFirst
- {
- get { return this.CurrentPageIndex > 1; }
- }
- /// <summary>
- /// 能跳转到最后一个
- /// </summary>
- public bool CanGoLast
- {
- get { return this.CurrentPageIndex < TotalPageCount; }
- }
- #region 相关事件处理
- /// <summary>
- /// 是否是内部编辑
- /// </summary>
- private bool InnerEditing { get; set; }
- public void BeginInEdit()
- {
- InnerEditing = true;
- }
- public void EndInEdit()
- {
- InnerEditing = false;
- }
- public void SetCurrentPageIndex(int index)
- {
- ChangePageIndex(this.CurrentPageIndex, index);
- }
- private void ChangePageIndex(int oldIndex, int newIndex)
- {
- if (InnerEditing)
- return;
- try
- {
- BeginInEdit();
- //index相同也要重新执行,当刷新
- var eventArgs = new PageChangingEventArgs() { OldPageIndex = oldIndex, NewPageIndex = newIndex };
- eventArgs.RoutedEvent = PageIndexChangingEvent;
- eventArgs.Source = this;
- this.RaiseEvent(eventArgs);
- if (!eventArgs.IsCancel)
- {
- this.CurrentPageIndex = newIndex;
- var eventChangedArgs = new PageChangedEventArgs() { CurrentPageIndex = newIndex};
- eventChangedArgs.RoutedEvent = PageIndexChangedEvent;
- eventChangedArgs.Source = this;
- this.RaiseEvent(eventChangedArgs);
- }
- else
- {
- this.CurrentPageIndex = oldIndex;
- }
- }
- finally
- {
- EndInEdit();
- NotifyBtnEnabled();
- }
- }
- public event EventHandler<PageChangingEventArgs> PageIndexChanging
- {
- add { this.AddHandler(PageIndexChangingEvent, value); }
- remove { this.RemoveHandler(PageIndexChangingEvent, value); }
- }
- public event EventHandler<PageChangedEventArgs> PageIndexChanged
- {
- add { this.AddHandler(PageIndexChangedEvent, value); }
- remove { this.RemoveHandler(PageIndexChangedEvent, value); }
- }
- #endregion
- #region INotifyPropertyChanged成员
- public event PropertyChangedEventHandler PropertyChanged;
- public void NotifyPropertyChanged(string propertyName)
- {
- if (this.PropertyChanged != null)
- {
- this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public void NotifyBtnEnabled()
- {
- NotifyPropertyChanged(nameof(CanGoFirst));
- NotifyPropertyChanged(nameof(CanGoLast));
- }
- #endregion
- }
- }
|