扩展SilverLight的DataPager控件 (转)
时间:2011-01-19 来源:slwf7man
其实扩展DataPager很简单,只要获取到DataPager控件上的元素,然后再改变元素上数据。比如DataPager控件上显示“总页数”的元素是一个TextBlock,那么可以通过方法GetTemplateChild获取到,参数是元素的名称。然后通过重写方法OnApplyTemplate即可,下面请看代码
代码
/// <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;
}
}
/// 扩展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;
}
}
有人可能不知道怎么知道控件DataPager上元素的名称,比如"CurrentPagePrefixTextBlock",其实很简单,你只要查询DataPager元数据即可。
通过上面的代码,就已经扩展了SL的控件DataPager,然后就可以像使用普通的DataPager一样使用,但是如果想要显示数据总数,必须向属性DataCount赋值。
希望以上对大家有帮助。
相关阅读 更多 +