[英]How to implement a data-bound control with templates?
I want to implement a very simple list control. 我想实现一个非常简单的列表控件。 It has 2 properties: 它具有2个属性:
The control implementation must render each row using the template specified in RowTemplate
passing the corresponding object in the DataSource
list, so that, if I have a property called Name
in the passed object, it would be accessible with a Eval("Name")
. 控件实现必须使用在RowTemplate
指定的模板来呈现每一行,并在DataSource
列表中传递相应的对象,以便如果我在传递的对象中具有名为Name
的属性,则可以使用Eval("Name")
进行访问。
I want an example of how to render the control using RowTemplate
and how to pass the correspondent DataSource
object to the template. 我想要一个如何使用RowTemplate
呈现控件以及如何将对应的DataSource
对象传递到模板的示例。
I'm reading the MSDN documentation on the subject: Data Binding Expression Overview and Binding to Databases but I just can't find how to implement a control that passes an object to the template. 我正在阅读有关以下主题的MSDN文档: 数据绑定表达式概述和与数据库的绑定,但我只是找不到如何实现将对象传递给模板的控件。
It sounds like you're trying to develop a templated data-bound control. 听起来您正在尝试开发模板化的数据绑定控件。
Essentially you just need to iterate your datasource object during the rendering of the main control. 本质上,您只需要在呈现主控件期间迭代数据源对象。 For each item in your datasource, create a new instance of your template container object and add it to the container control. 对于数据源中的每个项目,创建一个模板容器对象的新实例,并将其添加到容器控件中。 Depending on what you're extending, the actual render method might vary, but if you're extending something like BaseDataList
, then you would do it in " CreateControlHeirarchy()
". 根据您要扩展的内容,实际的渲染方法可能会有所不同,但是如果您要扩展BaseDataList
类的BaseDataList
,则可以在“ CreateControlHeirarchy()
”中进行。
There's a few walkthroughs around that you can check out. 您可以查看一些演练。 Here's a pretty straightforward one . 这是一个非常简单的例子 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.