如何从List中获取Object元素并将它们添加到一起

时间:2015-10-02 13:38:44

标签: c# binding

我需要从List<Ingredient> myIngredient和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);
    }
}

这是类成分

    class Ingredient
    {

        public string ToppingName { get; set; }
        public string UnitName { get; set; }
        public int Cost { get; set; }
        public string DescriptionName { get; set; }
    int totalCost;


        public Ingredient(string pizzaToppingName, string pizzaToppingUnit, int pizzaCost, string pizzaDescription)
        {
            ToppingName = pizzaToppingName;
            UnitName = pizzaToppingUnit;
            Cost = pizzaCost;
            DescriptionName = pizzaDescription;
        }

    public string DisplayName
    {
        get
        {
           return ToppingName + " " + UnitName;
        }
    }

   public override string ToString()
    {
      return string.Format("{0} - {1} ${2:F}", ToppingName, UnitName, Cost);
    }

}

1 个答案:

答案 0 :(得分:0)

嗯...为了让您入门 - 您不希望将列表项单独添加到复选框列表中。您希望将整个列表添加为数据源:

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控件列表项引用的基础数据项。例如,您可以从列表中选择所选的成分 - 比如标有&#34;订购披萨&#34;的按钮:

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...
}