C# 如何从列表中获取对象元素并将它们添加到一起

C# 如何从列表中获取对象元素并将它们添加到一起,c#,binding,C#,Binding,我需要把价格从我的配料表中拿出来,放到Windows窗体上的标签中。到目前为止,代码就是我所拥有的,我需要添加什么来实现这一点,我已经做了好几天了,但似乎无法实现这一点。在处理控制台应用程序时,我很好,但窗口形式有点难 这是Windows窗体页 public partial class Form1 : Form { List<Ingredient> myIngredient = new List<Ingredient>(); public Form1

我需要把价格从我的配料表中拿出来,放到Windows窗体上的标签中。到目前为止,代码就是我所拥有的,我需要添加什么来实现这一点,我已经做了好几天了,但似乎无法实现这一点。在处理控制台应用程序时,我很好,但窗口形式有点难

这是Windows窗体页

public partial class Form1 : Form
{

    List<Ingredient> myIngredient = new List<Ingredient>();


    public Form1()
    {
        InitializeComponent();

        myIngredient.Add(new Ingredient("Cheese", "grams", 3, "grab cheese and sprinkle on top of pizza"));
        myIngredient.Add(new Ingredient("Olives", "grams", 2, "cut up olives and sprickle on pizza"));
        myIngredient.Add(new Ingredient("Ham", "grams", 1, "spread cut up ham over pizza"));
        myIngredient.Add(new Ingredient("Pineapple", "grams", 1, "spread cut up chunks over pizza"));
        myIngredient.Add(new Ingredient("Pepperoni", "pieces", 1, "place slices on pepperoni on pizza"));
        myIngredient.Add(new Ingredient("Onion", "handfuls", 1, "sprinkle cut up onion on pizza, try not to cry"));
        myIngredient.Add(new Ingredient("Peppers", "grams", 1, "sprinkle cut up peppers on pizza"));
        myIngredient.Add(new Ingredient("Anchovy", "grams", 1, "place on top of pizza"));
        myIngredient.Add(new Ingredient("Mushrooms", "grams", 1, "put gently on top of pizza"));



        foreach (Ingredient pizza in myIngredient)
            if (pizza != null)
            {
                checkedListBoxIngredients.Items.Add(myIngredient);
            }
        checkedListBoxIngredients.DataSource = myIngredient;
        checkedListBoxIngredients.DisplayMember = "DisplayName";



    }


    private void checkedListBoxIngredients_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        string item = checkedListBoxIngredients.SelectedItem.ToString();

        if (e.NewValue == CheckState.Checked)
        {
            yourPizza.Items.Add(item);

        }

        else
            yourPizza.Items.Remove(item);
    }
}

嗯…为了让您开始-您不想将列表项单独添加到复选框列表中。要将整个列表添加为数据源:

public Form1( )
{
  InitializeComponent( );

  myIngredient.Add( new Ingredient( "Cheese", "grams", 3, "grab cheese and sprinkle on top of pizza" ) );
  myIngredient.Add( new Ingredient( "Olives", "grams", 2, "cut up olives and sprickle on pizza" ) );
  myIngredient.Add( new Ingredient( "Ham", "grams", 1, "spread cut up ham over pizza" ) );
  myIngredient.Add( new Ingredient( "Pineapple", "grams", 1, "spread cut up chunks over pizza" ) );
  myIngredient.Add( new Ingredient( "Pepperoni", "pieces", 1, "place slices on pepperoni on pizza" ) );
  myIngredient.Add( new Ingredient( "Onion", "handfuls", 1, "sprinkle cut up onion on pizza, try not to cry" ) );
  myIngredient.Add( new Ingredient( "Peppers", "grams", 1, "sprinkle cut up peppers on pizza" ) );
  myIngredient.Add( new Ingredient( "Anchovy", "grams", 1, "place on top of pizza" ) );
  myIngredient.Add( new Ingredient( "Mushrooms", "grams", 1, "put gently on top of pizza" ) );



  checkedListBoxIngredients.DataSource = new BindingSource( myIngredient, null );
  checkedListBoxIngredients.DisplayMember = "DisplayName";

}
BindingSource是键。你会想查一下的

将list-ish控件绑定到list-ish数据源时,可以直接访问UI控件的列表项引用的基础数据项。例如,您可以从列表中获取选定的配料-比如在标有“订购比萨饼”的按钮上:

private void orderPizza_Click( object sender, EventArgs e )
{

  var selectedIngredients =
    new List<Ingredient>
    (
      checkedListBoxIngredients
      .CheckedItems
      .Cast<Ingredient>()
    );

  var ingredientPrice = selectedIngredients.Sum( i=> i.Cost );

  //--> complete the order...
}

label1.Text=mycomponent.Sumx=>x.Cost.toString如何将其绑定到窗口窗体?感谢您的快速回复这可能有助于winforms中的绑定:
private void orderPizza_Click( object sender, EventArgs e )
{

  var selectedIngredients =
    new List<Ingredient>
    (
      checkedListBoxIngredients
      .CheckedItems
      .Cast<Ingredient>()
    );

  var ingredientPrice = selectedIngredients.Sum( i=> i.Cost );

  //--> complete the order...
}