简体   繁体   English

绑定列表框到列表<object>在 WinForms 中

[英]Binding Listbox to List<object> in WinForms

将列表框绑定到 Windows 窗体中的对象列表的最简单方法是什么?

You're looking for the DataSource property :您正在寻找DataSource property

List<SomeType> someList = ...;
myListBox.DataSource = someList;

You should also set the DisplayMember property to the name of a property in the object that you want the listbox to display.您还应该将DisplayMember属性设置为您希望列表框显示的对象中的属性名称。 If you don't, it will call ToString() .如果不这样做,它将调用ToString()

Binding a System.Windows.Forms.Listbox Control to a list of objects (here of type dynamic)System.Windows.Forms.Listbox控件绑定到对象列表(此处为动态类型)

List<dynamic> dynList = new List<dynamic>() { 
            new {Id = 1, Name = "Elevator", Company="Vertical Pop" },
            new {Id = 2, Name = "Stairs", Company="Fitness" }
};

listBox.DataSource = dynList; 
listBox.DisplayMember = "Name";
listBox.ValueMember = "Id";  

Pretending you are displaying a list of customer objects with "customerName" and "customerId" properties:假设您正在显示具有“customerName”和“customerId”属性的客户对象列表:

listBox.DataSource = customerListObject;
listBox.DataTextField = "customerName";
listBox.DataValueField = "customerId";
listBox.DataBind();

Edit: I know this works in asp.net - if you are doing a winforms app, it should be pretty similar (I hope...)编辑:我知道这在 asp.net 中有效 - 如果你正在做一个 winforms 应用程序,它应该非常相似(我希望......)

Granted, this isn't going to provide you anything truly meaningful unless the objects have properly overriden ToString() (or you're not really working with a generic list of objects and can bind to specific fields):当然,这不会为您提供任何真正有意义的东西,除非对象正确覆盖了ToString() (或者您没有真正使用通用对象列表并且可以绑定到特定字段):

List<object> objList = new List<object>();

// Fill the list

someListBox.DataSource = objList;
ListBox1.DataSource = CreateDataSource();
ListBox1.DataTextField = "FieldProperty";
ListBox1.DataValueField = "ValueProperty";

Please refer to this article for detailed examples.请参考文章详细的例子。

I haven 't seen it here so i post it because for me is the best way in winforms:我还没有在这里看到它,所以我发布了它,因为对我来说是 winforms 中的最佳方式:

    List<object> objList = new List<object>();

    listBox.DataSource = objList ;

    listBox.Refresh();
    listBox.Update();            

There are two main routes here:这里有两条主要路线:

1: listBox1.DataSource = yourList; 1: listBox1.DataSource = yourList;

Do any manipulation (Add/Delete) to yourList and Rebind.对 yourList 和 Rebind 进行任何操作(添加/删除)。
Set DisplayMember and ValueMember to control what is shown.设置 DisplayMember 和 ValueMember 来控制显示的内容。

2: listBox1.Items.AddRange(yourList.ToArray()); 2: listBox1.Items.AddRange(yourList.ToArray());

(or use a for-loop to do Items.Add(...) ) (或使用 for 循环来执行Items.Add(...)

You can control Display by overloading ToString() of the list objects or by implementing the listBox1.Format event.您可以通过重载列表对象的 ToString() 或通过实现 listBox1.Format 事件来控制 Display。

For a UWP app:对于 UWP 应用:

XAML XAML

<ListBox x:Name="List" DisplayMemberPath="Source" ItemsSource="{x:Bind Results}"/>

C# C#

public ObservableCollection<Type> Results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM