简体   繁体   中英

How to implement a data-bound control with templates?

I want to implement a very simple list control. It has 2 properties:

  1. RowTemplate (ITemplate)
  2. DataSource (IList)

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") .

I want an example of how to render the control using RowTemplate and how to pass the correspondent DataSource object to the template.


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.

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() ".

There's a few walkthroughs around that you can check out. Here's a pretty straightforward one .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM