简体   繁体   English

返回System.Object

[英]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);
    } 
}

ValueMember of 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 更多细节

To List all the name 列出所有名称

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.

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