使用事件处理程序将项添加到组合框

时间:2015-11-12 21:13:40

标签: c# wpf combobox event-handling

我正在尝试使用按钮单击形式的事件处理程序将新项添加到组合框中。但是,我无法使用我创建的类更新组合框。该类从事件处理程序add_menu_item_click中获取字符串和int,我的事件和类如下:

private void add_menu_item_Click(object sender, RoutedEventArgs e)
{
    new_combobox_item item = new new_combobox_item();
    item.item_name = new_food.Text;
    item.price = int.Parse(food_price.Text);
    Menu.item.Add(item);
    //MessageBox.Show((menulist.SelectedItem as new_combobox_item).Value.ToString());
}

用于存储数据的类:

public class new_combobox_item
{
  private string _item_name;
  private Boolean _vegitarian;
  private int _price;
  public string item_name
  {
      get
      {
          return _item_name;
      }
      set
      {
          _item_name = value;
      }
  }

  public Boolean vegitarian
  {
      get
      {
          return _vegitarian;
      }
      set
      {
          _vegitarian = value;
      }
  }

   public int price
   {
       get
       {
           return _price;
       }
       set
       {
           _price = value;
       }
   }
   public override string ToString()
   {
       return _item_name;
   }

和我想要更新的组合框:

    private void ComboBox_MenuLoaded(object sender, RoutedEventArgs e)
    {
        List<String> Menu_item = new List<String>();
        Menu_item.Add("mac and cheese");
        Menu_item.Add("Veggie Burger");
        Menu_item.Add("Lasagne");
        //Menu_item.Add(new_combobox_item.item_name);
        //combobox reference
        var menulist = sender as ComboBox;
        //assigns items to the list
        menulist.ItemsSource = Menu_item;
        //item selected
        menulist.SelectedIndex = 0;
    }

非常感谢任何帮助/建议。

0 个答案:

没有答案