/// <summary>
/// 擴展DataPager類,一是要顯示總數據數有多少,二是修改TextBox的寬度
/// </summary>
public class ExtendDataPager : DataPager
{
//定義變量
TextBlock tbCurrentPagePrefix;
TextBlock tbCurrentPageSuffix;
Button btnNextPageButton;
Button btnFirstPageButton;
Button btnLastPageButton;
Button btnPreviousPageButton;
TextBox txtCurrentPageTextBox;
int _dataCount = 0;
/// <summary>
/// 取得數據總數
/// </summary>
public int DataCount
{
get { return _dataCount; }
set { _dataCount = value; }
}
double _CurrentPageTextBoxWidth = 55;
/// <summary>
/// 顯示當前頁的TextBox的寬度,默認寬度為55
/// </summary>
public double CurrentPageTextBoxWidth
{
get { return _CurrentPageTextBoxWidth; }
set { _CurrentPageTextBoxWidth = value; }
}
public ExtendDataPager():base()
{
}
/// <summary>
/// 重寫 當應用新模板時生成 System.Windows.Controls.DataPager 控件的可視化樹。
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
//通過名稱取得模板中的元素
tbCurrentPagePrefix = GetTemplateChild("CurrentPagePrefixTextBlock") as TextBlock;
tbCurrentPageSuffix = GetTemplateChild("CurrentPageSuffixTextBlock") as TextBlock;
btnNextPageButton = GetTemplateChild("NextPageButton") as Button;
btnFirstPageButton = GetTemplateChild("FirstPageButton") as Button;
btnLastPageButton = GetTemplateChild("LastPageButton") as Button;
btnPreviousPageButton = GetTemplateChild("PreviousPageButton") as Button;
txtCurrentPageTextBox = GetTemplateChild("CurrentPageTextBox") as TextBox;
//重寫以下元素的事件
btnNextPageButton.Click += new RoutedEventHandler(
(o, e) =>
{
ExtendItem();
}
);
btnFirstPageButton.Click += new RoutedEventHandler(
(o, e) =>
{
ExtendItem();
}
);
btnLastPageButton.Click += new RoutedEventHandler(
(o, e) =>
{
ExtendItem();
}
);
btnPreviousPageButton.Click += new RoutedEventHandler(
(o, e) =>
{
ExtendItem();
}
);
txtCurrentPageTextBox.KeyDown += new KeyEventHandler(
(o, e) =>
{
ExtendItem();
}
);
ExtendItem();
}
/// <summary>
/// 擴展項
/// </summary>
private void ExtendItem()
{
//重寫以下元素顯示的內容以及顯示當前頁的TextBox的寬度
tbCurrentPagePrefix.Text = "第";
tbCurrentPageSuffix.Text = "頁 共" + this.PageCount.ToString() + "頁 共"+DataCount.ToString()+"條數據";
txtCurrentPageTextBox.Width = CurrentPageTextBoxWidth;
}
}