繁体   English   中英

将DropDownList绑定到Objects集合中的集合

[英]Bind DropDownList to a collection in a collection of Objects

我试图将Dropdownlist绑定到对象列表中包含的集合中的属性。

我有一个带有属性集合的对象,因此,如果我有该对象的单个实例,则可以这样调用该属性:

MyObject.Properties["Title"].Value.ToString();

现在我有一个

List<MyObject>

并且我想将Properties [“ Title”]。Value.ToString绑定到下拉列表中的DataValueField。

dropDownList.DataSource = List<MyObject>
dropDownList.DataValueField = "Title";
dropDownList.DataBind();

当然这是行不通的,因为Title在Properties集合中,而不是对象的属性。

dropDownList.DataValueField = "Properties[\"Title\"]"

也不起作用,因为Properties [“ Title”]也不是对象的属性。 我尝试了其他几个变体,但没有任何运气。 有没有办法做到这一点,或者我需要遍历对象,列出键值对的列表/字典并使用它。 在我看来,应该有一种方法可以做到这一点。 任何帮助表示赞赏

我只是使用Linq Select来设置一个匿名对象:

dropDownList.DataSource = myObjectList
    .Select(item => new { Title = item.Properties["Title"] })
    .ToList();
dropDownList.DataValueField = "Title";

暂无
暂无

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

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