大家一定遇到这样的情况,想改变一下SL的DataPager的显示信息,比如希望分页控件上显示数据的总数。那么就需要扩展一下DataPager控件即可。

  其实扩展DataPager很简单,只要获取到DataPager控件上的元素,然后再改变元素上数据。比如DataPager控件上显示“总页数”的元素是一个TextBlock,那么可以通过方法GetTemplateChild获取到,参数是元素的名称。然后通过重写方法OnApplyTemplate即可,下面请看代码

代码

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> /// <summary>
/// 扩展DataPager类,一是要显示总数据数有多少,二是修改TextBox的宽度
/// </summary>
public class ExtendDataPager : DataPager
{
//定义变量
TextBlock tbCurrentPagePrefix;
TextBlock tbCurrentPageSuffix;
Button btnNextPageButton;
Button btnFirstPageButton;
Button btnLastPageButton;
Button btnPreviousPageButton;
TextBox txtCurrentPageTextBox; int _dataCount = ; /// <summary>
/// 取得数据总数
/// </summary>
public int DataCount
{
get { return _dataCount; }
set { _dataCount = value; }
} double _CurrentPageTextBoxWidth = ; /// <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赋值。

希望以上对大家有帮助。

本文转自:http://www.cnblogs.com/888h/archive/2010/10/26/1861782.html

Silverlight中DataPager控件扩展的更多相关文章

  1. Silverlight中Image控件Stretch属性的四种值比较

    通过设置Image控件Stretch属性的值可以控制图片的显示形式: 包含的值:None.Fill.Uniform.UniformToFill <Grid x:Name="Layout ...

  2. Silverlight中获取控件中子控件

    如题:,直接来看代码: /// <summary> /// 查找并返回第一个 相同 name的子元素 /// </summary> /// <typeparam name ...

  3. Silverlight 中datagrid控件-- 通过设置数据虚拟化加速显示

    定义依赖属性作为datagrid的数据源 protected static readonly DependencyProperty ViewLogsProperty = DependencyPrope ...

  4. WPF中查找控件的扩展类

    在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...

  5. 玩转控件:对Dev中GridControl控件的封装和扩展

    又是一年清明节至,细雨绵绵犹如泪光,树叶随风摆动.... 转眼间,一年又过去了三分之一,疫情的严峻让不少企业就跟清明时节的树叶一样,摇摇欲坠.裁员的裁员,降薪的降薪,996的996~~说起来都是泪,以 ...

  6. 《ASP.NET1200例》ListView 控件与DataPager控件的结合<二>

    ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示 为什么使用ListView+DataPager的方式实现分页显示? .net提供的诸多数据绑定控件,每一种都有它自己 ...

  7. 《ASP.NET1200例》ListView 控件与DataPager控件的结合<一>

    分页     在前一部分开始时介绍的原 HTML 设计中内含分页和排序,所以根据规范完整实现该网格的任务尚未完成.我们先分页,然后再排序. ListView 控件中的分页通过引入另一个新控件 Data ...

  8. ArcGIS Engine开发之旅03--ArcGIS Engine中的控件

    原文:ArcGIS Engine开发之旅03--ArcGIS Engine中的控件 制图控件,如MapControl.PageLayoutControl,其中MapControl控件主要用于地理数据的 ...

  9. vb.net中让控件内容为空(Control类)

    在平常的敲系统中大家有没有遇到需要让Textbox控件或者其他的控件的显示内容为空,以前直接的做法是直接等于空值,如果此类控件有很多,都需要空值,难道都要设置一下它的值为空嘛,显然这是一个笨办法,有没 ...

随机推荐

  1. C++学习41 exception类

    C++语言本身或者标准库抛出的异常都是 exception 的子类,称为标准异常(Standard Exception).你可以通过下面的语句来匹配所有标准异常: try{ //可能抛出异常的语句 } ...

  2. 2013年山东省第四届ACM大学生程序设计竞赛-最后一道大水题:Contest Print Server

    点击打开链接 2226: Contest Print Server Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 53  Solved: 18 [Su ...

  3. VS2013配置WTL90_4140_Final

    网上关于WTL的文章,尤其是中文的文章不多,根据收集的资料整理出了VS2013安装WTL的方法. 1.下载.文件很小的,地址:http://sourceforge.net/projects/wtl/f ...

  4. (整理)C#实现UDP广播

    (一) IP地址解析 IP地址的类型:XXX.XXX.XXX.XXXA类: 1.0.0.1--126.255.255.254 最高位是0: 1个字节的网络地址和3个地址的主机地址 测试地址:127.X ...

  5. Throttling ASP.NET Web API calls

    http://blog.maartenballiauw.be/post/2013/05/28/Throttling-ASPNET-Web-API-calls.aspx https://github.c ...

  6. Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    B. Making Sequences is Fun time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. DP走方格型

    Hrbust 1812  http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1812 有 ...

  8. Android设置透明、半透明等效果

    设置透明效果 大概有三种 1.用android系统的透明效果Java代码 android:background="@android:color/transparent"  例如 设 ...

  9. (Loadrunner)Abnormal termination, caused by mdrv process termination.(转)

    Load generator跑了太多用户导致CPU和内存爆满,进程无法处理请求 确认自定义的代码是否释放内存 合理调整或增加思考时间 关闭extended log 尽量避免使用Load generat ...

  10. 强大的内网劫持框架之MITMf

    Mitmf 是一款用来进行中间人攻击的工具.它可以结合 beef 一起来使用,并利用 beef 强大的 hook 脚本来控制目标客户端.下面让我们一起看看如何在 Kali2.0上安装使用 Mitmf ...