简体   繁体   English

组合框显示对象子成员

[英]combobox display object children member

I'm trying to fill a ComboBox using a list of objects. 我正在尝试使用对象列表填充ComboBox。 Each object of the list has another objects. 列表中的每个对象都有另一个对象。 I need to show "option.Dish.dish_name" in the comboBox DisplayMember and I need "option.option_id" in the comboBox ValueMember. 我需要在comboBox DisplayMember中显示“ option.Dish.dish_name”,并且需要在comboBox ValueMember中显示“ option.option_id”。 Here is what I'm trying to do: 这是我想要做的:

foreach (Option option in optionList)
{
        this.comboBox1.Items.Add(option);
        this.comboBox1.DisplayMember = "Dish.dish_name";
        this.comboBox1.ValueMember = "option_id";
 }

The option class is an Entity Framework Entity, but is something like: 选项类是实体框架实体,但类似于:

public class Option 
{
    private int option_id;
    private DateTime option_date;
    private Dish dish;
}

public class Dish
{
    private int dish_id;
    private String dish_name;
    private DateTime date_created;
}

Thanks in advance. 提前致谢。

EDIT 编辑

New problem: There is anyway to get that "new object" to use it later? 新问题:总有办法让“新对象”在以后使用吗? I need another value, for example: 我需要另一个值,例如:

this.comboBox1.ItemsSource = optionList.Select(o=> new
{
    option_id = o.option_id, 
    dish_name = o.dish.dish_name, 
    date = o.dish_date
}).ToList(); 

and then use that object to get the "date" value?. 然后使用该对象获取“日期”值?

this.comboBox1.ItemsSource =
        optionList.Select(o=> new{option_id = o.option_id, dish_name =o.dish.dish_name}).ToList();

this.comboBox1.DisplayMember = "dish_name";
this.comboBox1.ValueMember = "option_id";

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

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