[英]Returns System.Object
I'm passing a List<object>
to a method that populates a checkedListBox
. 我将
List<object>
传递给填充checkedListBox
的方法。 My understanding is Object.ToString()
associates the object with the checkedListBox
item and displays readable string. 我的理解是
Object.ToString()
将对象与checkedListBox
项目关联,并显示可读的字符串。 Object.Name accomplishes this, but then the data isn't associated with the item. Object.Name完成了此操作,但随后数据未与该项目关联。 How do I access the
ValueMember
property of the checkListBox
item? 如何访问
checkListBox
项的ValueMember
属性?
public void CategoryListBox(List<Category> categoryList)
{
checkedListBox1.Items.Clear();
foreach (Category category in categoryList)
{
checkedListBox1.Items.Add(category);
}
}
CheckBoxList
CheckBoxList
ValueMember From the answer of Mr. codingbiz I did some quick research for this control checkListBox
about ValueMember
Property, so obviously from Intellisense we can't navigate this .DisplayMember
and .ValueMember
for checkListBox
, so I figured it out that this Property is already included in checkListBox
but not showing in it. 从codingbiz先生的答案中,我对有关
ValueMember
属性的控件checkListBox
进行了一些快速研究,因此很明显,从Intellisense中,我们无法针对checkListBox
导航此.DisplayMember
和.ValueMember
,因此我发现此属性已包含在其中。 checkListBox
但未显示在其中。 For more details 更多细节
By using Linq we can achieve this. 通过使用Linq,我们可以实现这一目标。
public class Category
{
public int Id { get; set; }
public string CategoryName { get; set; }
}
List<Category> category = new List<Category>();
category.Add(new Category { Id = 1, CategoryName = "test" });
category.Add(new Category { Id = 2, CategoryName = "let" });
category.Add(new Category { Id = 3, CategoryName = "go" });
checkedListBox1.DataSource = category;
checkedListBox1.DisplayMember = "CategoryName";
//checkedListBox1.ValueMember = "Id";
//checkedListBox1.Items.AddRange(category.Select(c => c.CategoryName).ToArray());
I hope you can get some idea from this answer. 我希望您能从这个答案中得到一些想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.