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
{
///
/// 导航分页
///
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), typeof(NavigationPager));
public static readonly RoutedEvent PageIndexChangedEvent = EventManager.RegisterRoutedEvent("PageIndexChanged",
RoutingStrategy.Direct, typeof(EventHandler), 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));
}
///
/// 当前页索引
///
public int CurrentPageIndex
{
get { return (int)GetValue(CurrentPageIndexProperty); }
set { SetValue(CurrentPageIndexProperty, value); }
}
///
/// 总页数
///
public int TotalPageCount
{
get { return (int)GetValue(TotalPageCountProperty); }
set { SetValue(TotalPageCountProperty, value); }
}
///
/// 总页数是否显示
///
public bool TotalPageVisible
{
get { return (bool)GetValue(TotalPageVisibleProperty); }
set { SetValue(TotalPageVisibleProperty, value); }
}
///
/// 附加元素
///
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
///
/// 能跳转到第一个
///
public bool CanGoFirst
{
get { return this.CurrentPageIndex > 1; }
}
///
/// 能跳转到最后一个
///
public bool CanGoLast
{
get { return this.CurrentPageIndex < TotalPageCount; }
}
#region 相关事件处理
///
/// 是否是内部编辑
///
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 PageIndexChanging
{
add { this.AddHandler(PageIndexChangingEvent, value); }
remove { this.RemoveHandler(PageIndexChangingEvent, value); }
}
public event EventHandler 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
}
}