简体   繁体   English

将DataList绑定到对象的方法而不是属性

[英]Binding a DataList to an object's method instead of a property

I have a class that looks like this: 我有一个看起来像这样的课程:

public class Person
{
  public string Name { get; set; }

  public string Thing() { 
    ...
  }

  ...

}

If I have an IList<Person> that I'm using as a data source for a DataList control, and the DataList looks like this: 如果我有一个IList<Person> ,我将其用作DataList控件的数据源,并且DataList如下所示:

<asp:DataList runat="server" RepeatColumns="1" ID="Profiles">
    <ItemTemplate>                                  
        <%#Eval("Name") %>          
    </ItemTemplate>
</asp:DataList>

How do I replace the Name property of the data source with a call to the data source object's Thing() method? 如何通过调用数据源对象的Thing()方法替换数据源的Name属性?

<%#((Person)Container.DataItem).Thing()%>

Use properties. 使用属性。 The property will be "Name" (or whatever) and the "Getter" on this property will be the method you wish to use to generate the value. 该属性将为“Name”(或其他),此属性上的“Getter”将是您希望用于生成值的方法。 While you can call a function from here, I think it would be better design to use a property. 虽然你可以从这里调用一个函数,但我认为使用属性会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定到对象本身而不是属性时INotifyPropertyChanged - INotifyPropertyChanged when binding to object itself instead of a property WPF-将VM属性绑定到UI对象的方法 - WPF - Binding a VM property to a method of an UI object 绑定从返回 object 的方法获得的属性 - Binding a property obtained from a method, that returns object DataGridView绑定到对象的“ ToString”属性 - DataGridView binding to an object's “ToString” property 为什么Object.GetType()是一个方法而不是一个属性? - Why is Object.GetType() a method instead of a property? Ninject绑定基于对象的属性? 约定还是上下文约束? - Ninject binding based on object's property? Convention or contextual binding? 在不覆盖 Equals 的情况下绑定时通过属性而不是引用来识别对象 - Identify object by property instead of reference when binding without overriding Equals 将数据绑定到对象内部的属性,该对象由附加到绑定对象的扩展方法返回 - Databind to a property inside a object returned by a extension method attached to the binding object 将对象属性的引用传递给方法 - Pass a reference of an object's property to a method 将winforms控件的属性绑定到多个对象属性 - binding a winforms control's property to multiple object properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM