WPF和winform区别 combobox 填充list例子
区别:
var deviceEnum = new MMDeviceEnumerator();
var devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active).ToList();
combDevice.ItemsSource = devices;//wpf
combDevice.DataSource = devices;//WINFORM
------
comboBox1.DataSource = bindingSource1.DataSource;//WINFORM
生成ComboBox list代码例子:
1 List<string> list = new List<string>(); 2 BindingSource bsource=new BindingSource(); 3 4 //Set list dataSource 5 bsource.DataSource = list; 6 comboBox1.DataSource = bsource; 7 8 //Now add an element via Binding object 9 bsource.Add("One"); 10 bsource.Add("Two");
代码例子2:
1 public class Country 2 { 3 public string Name { get; set; } 4 public IList<City> Cities { get; set; } 5 public Country(string _name) 6 { 7 Cities = new List<City>(); 8 Name = _name; 9 } 10 } 11 12 13 14 List<Country> countries = new List<Country> { new Country("UK"), 15 new Country("Australia"), 16 new Country("France") }; 17 18 bindingSource1.DataSource = countries; 19 20 comboBox1.DataSource = bindingSource1.DataSource; 21 22 comboBox1.DisplayMember = "Name"; 23 comboBox1.ValueMember = "Name";
Or you may try ArrayList.Adapter method that creates Adapter wrapper of IList.
ArrayList items;
items=ArrayList.Adapter(comboBox1.Items);
items.Add("one");
其他
代码:
1 void Form1Load(object sender, EventArgs e) 2 { 3 List<string> myitems = new List<string> 4 { 5 "Item 1", 6 "Item 2", 7 "Item 3" 8 }; 9 10 ComboBox box = new ComboBox(); 11 box.Bounds = new Rectangle(10, 10, 100, 50); 12 source1.DataSource = myitems; 13 box.DataSource = source1; 14 15 ComboBox box2 = new ComboBox(); 16 box2.Bounds = new Rectangle(10, 80, 100, 50); 17 source2.DataSource = myitems; 18 box2.DataSource = source2; 19 20 Controls.Add(box); 21 Controls.Add(box2); 22 }
ref:
https://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms
https://stackoverflow.com/questions/482/winforms-combobox-data-binding-gotcha
其他区别:
private void btIATstop_Click(object sender, EventArgs e)
private void btnStop_Click(object sender, RoutedEventArgs e) //WPF
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~