大家一定遇到这样的情况,想改变一下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. spring错误<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX

    spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...

  2. Ext.Form 自动填写表单内容

    前台: 表单必须含有name属性 if (action == 'edit' || action == 'show') { MyForm1.getForm().load({ url: '/data/cu ...

  3. LeetCode Lowest Common Ancestor of a Binary Serach Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  4. win10 TortoiseSVN 部分图标不显示

    原因:https://msdn.microsoft.com/en-us/library/cc144123(VS.85).aspx Note   The number of different icon ...

  5. 推荐一个PHP在线代码运行的网站

    地址:http://www.manongjc.com/runcode 该网站可以运行php代码.html代码.js代码, 对于初学者来说,免去了安装环境这一步.

  6. JAVA Socket:文件传输

    客户端:读取文件(FileInputStream),发送文件(OutputStream) 服务器端:接收文件(InputStream),写文件(FileOutputStream) 客户端代码: pac ...

  7. Gradle用户指南(章9:Groovy快速入门)

    Gradle用户指南(章9:Groovy快速入门) 你可以使用groovy插件来构建groovy项目.这个插件继承了java插件的功能,且扩展了groovy编译.你的项目可以包含groovy代码.ja ...

  8. 创建母版页导致js出现“ 'document.getElementById(...)' 为空或不是对象”错误

    导读:一个控件在设计时的ID往往不同于生成页面后的ID,为了获得控件客户端ID,我们可以从生成的页面入手,冷静思考,把握主次,从底层框架入手 本文将为大家介绍一下 ASP.NET中在创建母版页时引来的 ...

  9. 合并同一目录下多个EXCEL的多个sheet

    合并同一目录下多个EXCEL的多个sheet到一个excel的一个sheet 1.把多个excel表都放在同一个文件夹里面,并在这个文件夹里面新建一个excel2.打开新建的excel表,并右键单击s ...

  10. android 上传文件"Content-Type",为"application/octet-stream" 用php程序在服务端用$GLOBALS['HTTP_RAW_POST_DATA']接受(二)

    服务端php程序file_up.php function uploadFileBinary() { $this->initData(); $absoluteName = "" ...