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");

其他ComboBox代码:
 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

 
 

原文地址:https://www.cnblogs.com/watermarks/p/8457840.html

时间: 2024-08-23 16:52:04

WPF和winform区别 combobox 填充list例子的相关文章

WPF和winform的区别

WPF,即windows presentation foundation,windows呈现基础,属于.net framework3.0,是微软推出取代winform的产品,能做到分离界面设计人员与开发人员的工作,提供多媒体交互用户图形界面,三大核心程序集是presentationcore.presentationFramework.windowsBase WPF和winform最大的区别在于WPF底层使用的DirectX,winform底层使用的是GDI+,所以WPF的图形界面上更胜一筹 GD

WPF与WinForm开发有什么区别?

转自http://hi.baidu.com/leoliu83/blog/item/1d1a4a66dcb41134aa184cfd.html WPF开发于WinForm之后,从技术发展的角度,WPF比WinForm先进是不容置疑的.我觉得WPF相比于WinForm有下面的一些较好的特性: 解决Window Handle问题   在Windows GDI或WinForm开发中复杂的GUI应用程序,会使用的大量的控件,如Grid等.而每个控件或Grid cell都是一个小窗口,会使用一个Window

编写高质量代码改善C#程序的157个建议——建议87:区分WPF和WinForm的线程模型

建议87:区分WPF和WinForm的线程模型 WPF和WinForm窗体应用程序都有一个要求,那就是UI元素(如Button.TextBox等)必须由创建它的那个线程进行更新.WinForm在这方面的限制并不是很严格,所以像下面这样的代码,在WinForm中大部分情况下还能运行(本建议后面会详细解释为什么会出现这种现象): private void buttonStartAsync_Click(object sender, EventArgs e) { Task t = new Task(()

WPF与WinForm的抉择

微软曾经对WPF(代号Avalon)抱很大的期望——新一代的华丽用户界面平台,一统Web应用和桌面应用,Flash杀手,尽管微软口头上不承认.几年下来,WPF确实实现了当初的预期的大部分功能,但离称霸软件开发领域还有不小的距离.不过,刚转到WinForm上一两年的开发人员们都很困惑,WinForm究竟还有没有未来.微软只是模糊地说短期内还是WinForm,长期是WPF.想想“维纳斯”,“TabletPC”,“Hailstorm”,“IE Channels”等等就知道这位老大的话不能全信.到底何去

c#(winform)中ComboBox添加Key/Value项、获取选中项、根据Key

WinForm下的ComboBox默认是以多行文本来设定显示列表的, 这通常不符合大家日常的应用, 因为大家日常应用通常是键/值对的形式去绑定它的. 参考了一些网上的例子,最终写了一个辅助类用于方便对ComboBox的操作: 用下面这个类的实例作为ComboBox的添加项: using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace tp7309.

Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决

有人会说不建议Wpf中使用Winform控件,有人会说建议使用Winform控件在Wpf下的替代方案,然而在实际工作中由于项目的特殊需求,考虑到时间.成本等因素,往往难免会碰到在WPF中使用Winfrom控件的问题,我们知道Wpf可以通过使用WindowsFormsHost容器调用Winform控件,但是在一些场合需要将Wpf元素显示在Winform控件的上层,此时就会出现Wpf元素被Winform控件遮盖的问题. 一.场景再现 接到公司命令,在时间紧迫的情况下,需要将原来的Winform程序(

WPF中DataGrid的ComboBox的简单绑定方式(绝对简单)

在写次文前先不得不说下网上的其他wpf的DataGrid绑定ComboBox的方式,看了之后真是让人欲仙欲死. 首先告诉你一大堆的模型,一大堆的控件模板,其实或许你紧紧只想知道怎么让combobox怎么显示出来而已. 惯例先上图: 达到这样的效果其实很简单,除了让数据模型之外紧紧只有几行代码. 先看数据模型: public class VModel : INotifyPropertyChanged { private string _Name; public string Name { get

GDI、DirectX、WPF、Winform等绘图相关关系梳理

绘图引擎方面,Windows下常用的为GDI.GDI+.DirectX.OpenGL等引擎,而Winform.MFC.WPF.Qt等则是用来实现对这些引擎调用的工具,属于更上层的范围,这几个绘图引擎中,前三者都是微软自己的东西,因此如果用C#来写的话用这几个引擎相对就会方便些. WPF采用的是DirectX作为底层绘图引擎,Winform和MFC则都采用GDI+,因此,Winform和MFC基本的绘图效率应该是接近的. DirectX是可以实现硬件加速的,如果PC上有GPU的话,那么WPF的图形

WPF - MVVM - 如何将ComboBox的Selectchange事件binding到ViewModel

将所有的事件,属性,都映射到ViewModel中.好处多多,以后开发尽量用这种模式. 解决方法: 使用System.Windows.Interactivity.dll,添加该dll到项目引用 ? 1 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ComboBox映射的代码: <ComboBox VerticalAlignment="Ce