轉(zhuǎn)帖|其它|編輯:郝浩|2011-01-20 15:04:09.000|閱讀 710 次
概述:大家一定遇到這樣的情況,想改變一下SL的DataPager的顯示信息,比如希望分頁控件上顯示數(shù)據(jù)的總數(shù)。那么就需要擴(kuò)展一下DataPager控件即可。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
大家一定遇到這樣的情況,想改變一下SL的DataPager的顯示信息,比如希望分頁控件上顯示數(shù)據(jù)的總數(shù)。那么就需要擴(kuò)展一下DataPager控件即可。
其實擴(kuò)展DataPager很簡單,只要獲取到DataPager控件上的元素,然后再改變元素上數(shù)據(jù)。比如DataPager控件上顯示“總頁數(shù)”的元素是一個TextBlock,那么可以通過方法GetTemplateChild獲取到,參數(shù)是元素的名稱。然后通過重寫方法OnApplyTemplate即可,下面請看代碼
/// <summary>
/// 擴(kuò)展DataPager類,一是要顯示總數(shù)據(jù)數(shù)有多少,二是修改TextBox的寬度
/// </summary>
public class ExtendDataPager : DataPager
{
//定義變量
TextBlock tbCurrentPagePrefix;
TextBlock tbCurrentPageSuffix;
Button btnNextPageButton;
Button btnFirstPageButton;
Button btnLastPageButton;
Button btnPreviousPageButton;
TextBox txtCurrentPageTextBox;
int _dataCount = 0;
/// <summary>
/// 取得數(shù)據(jù)總數(shù)
/// </summary>
public int DataCount
{
get { return _dataCount; }
set { _dataCount = value; }
}
double _CurrentPageTextBoxWidth = 55;
/// <summary>
/// 顯示當(dāng)前頁的TextBox的寬度,默認(rèn)寬度為55
/// </summary>
public double CurrentPageTextBoxWidth
{
get { return _CurrentPageTextBoxWidth; }
set { _CurrentPageTextBoxWidth = value; }
}
public ExtendDataPager():base()
{
}
/// <summary>
/// 重寫 當(dāng)應(yīng)用新模板時生成 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>
/// 擴(kuò)展項
/// </summary>
private void ExtendItem()
{
//重寫以下元素顯示的內(nèi)容以及顯示當(dāng)前頁的TextBox的寬度
tbCurrentPagePrefix.Text = "第";
tbCurrentPageSuffix.Text = "頁 共" + this.PageCount.ToString() + "頁 共"+DataCount.ToString()+"條數(shù)據(jù)";
txtCurrentPageTextBox.Width = CurrentPageTextBoxWidth;
}
}
有人可能不知道怎么知道控件DataPager上元素的名稱,比如"CurrentPagePrefixTextBlock",其實很簡單,你只要查詢DataPager元數(shù)據(jù)即可。
通過上面的代碼,就已經(jīng)擴(kuò)展了SL的控件DataPager,然后就可以像使用普通的DataPager一樣使用,但是如果想要顯示數(shù)據(jù)總數(shù),必須向?qū)傩訢ataCount賦值。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載