我有一个列出食谱名称的组合框.食谱保存在目录中.当用户点击组合时,我需要添加/删除项目并显示新的新下拉列表.我可以使用哪个事件来更新列表
我更喜欢以下方式.将您的食谱的ObservableCollection添加到类成员:
ObservableCollection<Recipe> recipeList = new ObservableCollection<Recipe>();
Run Code Online (Sandbox Code Playgroud)
设置组合框的数据源并订阅Click EventHandler:
comboBox1.DataSource = recipeList;
comboBox1.Click += new EventHandler(comboBox1_Click);
Run Code Online (Sandbox Code Playgroud)
在组合框的点击处理程序中,您可以将项目添加到列表中并"通过魔法"(由于可观察的模式)项目将显示在组合框中
void comboBox1_Click(object sender, EventArgs e)
{
recipeList.Add(new Recipe { Name = "Spagetti Bolognese" });
}
Run Code Online (Sandbox Code Playgroud)