简体   繁体   English

如何使用模板实现数据绑定控件?

[英]How to implement a data-bound control with templates?

I want to implement a very simple list control. 我想实现一个非常简单的列表控件。 It has 2 properties: 它具有2个属性:

  1. RowTemplate (ITemplate) RowTemplate(ITemplate)
  2. DataSource (IList) 数据源(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") . 控件实现必须使用在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.

相关问题 控件数据绑定时无法在DataGridView中以编程方式添加行 - Rows cannot be added programmatically in the DataGridView when the control data-bound 将IQueryable返回到数据绑定控件的正确语法是什么 - What is the correct syntax for returning IQueryable to a data-bound control 如何在数据绑定列表控件上实现主题? - How to implement theme on a data bound list control? 如何以编程方式设置数据绑定WPF ComboBox的SelectedItem? - How to programmatically set SelectedItem of a data-bound WPF ComboBox? 如何用图标呈现数据绑定的WinForms DataGridView列? - How to render a data-bound WinForms DataGridView Column with an Icon? 如何在c#中刷新数据绑定集合? - How to refresh a data-bound collection in c#? 如何在数据绑定时以编程方式向 datagridview 添加一行? - How to programmatically add a row to a datagridview when it is data-bound? 无法在数据绑定的 DataGridView 控件上设置 ColumnCount 属性。 c# - ColumnCount property cannot be set on a data-bound DataGridView control. c# 错误“当控件绑定数据时,行无法以编程方式添加到datagridview的行集合中” - Error “Rows cannot be programatically added to datagridview's row collection when the control is data-bound” 当控件是数据绑定的时,不能以编程方式将行添加到DataGridView的rows集合中 - Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM