C# 如何在 ListView 中插入对象类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10476902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:59:41  来源:igfitidea点击:

How to Insert Object Type in ListView?

c#winforms

提问by user662285

I want to maintain both ID and Object Type in my ListView. I'm trying to do this:

我想在我的 ListView 中同时维护 ID 和对象类型。我正在尝试这样做:

lstView.Items.Insert(MyObject);
// can't do this, because it takes only Int and String

In my case, the ID is int, so that part is ok. But how to insert an object type and retrieve it in the Item_Selection changed event?

在我的情况下,ID 是 int,所以这部分没问题。但是如何插入一个对象类型并在 Item_Selection changed 事件中检索它呢?

采纳答案by David Anderson

A ListViewcannot add or insert an object directly like a ListBoxor ComboBox, but instead you need to create a ListViewItemand set its Tagproperty.

AListView不能像 aListBox或那样直接添加或插入对象ComboBox,而是需要创建 aListViewItem并设置其Tag属性。

The Tag property from Msdn

来自 Msdn 的 Tag 属性

An Object that contains data about the control. The default is null.

Any type derived from the Object class can be assigned to this property. If the Tag property is set through the Windows Forms designer, only text can be assigned. A common use for the Tag property is to store data that is closely associated with the control. For example, if you have a control that displays information about a customer, you might store a DataSet that contains the customer's order history in that control's Tag property so the data can be accessed quickly.

包含有关控件的数据的对象。默认值为空。

从 Object 类派生的任何类型都可以分配给此属性。如果 Tag 属性是通过 Windows 窗体设计器设置的,则只能分配文本。Tag 属性的一个常见用途是存储与控件密切相关的数据。例如,如果您有一个显示客户信息的控件,您可以在该控件的 Tag 属性中存储一个包含客户订单历史记录的 DataSet,以便可以快速访问数据。

Example code:

示例代码:

MyObject myObj = new MyObject();

ListViewItem item = new ListViewItem();
item.Text = myObj.ToString(); // Or whatever display text you need
item.Tag = myObj;

// Setup other things like SubItems, Font, ...

listView.Items.Add(item);

When you need to get your object back from the ListView, you can cast the Tagproperty.

当您需要从 中取回您的对象时ListView,您可以强制转换该Tag属性。

private void OnListViewItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
    MyObject myObj = (MyObject)e.Item.Tag;
    int id = myObj.Id;

    // Can access other MyObject Members
}

Usually its easier to wrap the functionality into a helper method.

通常将功能包装到辅助方法中更容易。

public static void CreateListViewItem(ListView listView, MyObject obj) {
    ListViewItem item = new ListViewItem();
    item.Tag = obj;

    // Other requirements as needed

    listView.Items.Add(item);
}

And you can do:

你可以这样做:

CreateListViewItem(listView, obj);

A ListViewdoesn't support a DataSourceproperty like a lot of controls, so if you wish to data bind you will need to implement something a bit more custom.

AListView不支持DataSource像很多控件那样的属性,因此如果您希望数据绑定,您将需要实现一些更自定义的东西。

回答by Derreck Dean

Quickest way around this is to keep a list of your object on the side:

解决此问题的最快方法是将您的对象列表放在一边:

List<MyObject> list = ... ; // my list

Generate a dictionary from the list with a string as the ID, or you can use the index to retrieve from the original list:

从列表中生成一个以字符串为 ID 的字典,或者您可以使用索引从原始列表中检索:

Dictionary<int,string> listBinder = new Dictionary<int,string>(
    list.Select(i => new KeyValuePair<int,string>(i.ID, i.Name))
);

Bind or codebehind attaching the listview to the dictionary, then use the selected item to retrieve your object from your private list.

绑定或代码隐藏将列表视图附加到字典,然后使用所选项目从您的私人列表中检索您的对象。

回答by Vetrivel mp

Create new listviewitem object. use Tag property.

创建新的 listviewitem 对象。使用标签属性。